Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: improve Buffer.from performance #15178

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
if (isAnyArrayBuffer(value))
return fromArrayBuffer(value, encodingOrOffset, length);

if (value == null) {
if (value === null || value === undefined) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE',
'first argument',
Expand All @@ -208,7 +208,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
);

const valueOf = value.valueOf && value.valueOf();
if (valueOf != null && valueOf !== value)
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
return Buffer.from(valueOf, encodingOrOffset, length);

var b = fromObject(value);
Expand Down Expand Up @@ -322,9 +322,9 @@ function allocate(size) {
function fromString(string, encoding) {
var length;
if (typeof encoding !== 'string' || encoding.length === 0) {
encoding = 'utf8';
if (string.length === 0)
return new FastBuffer();
encoding = 'utf8';
length = byteLengthUtf8(string);
} else {
length = byteLength(string, encoding, true);
Expand Down