Skip to content

Commit

Permalink
buffer: refactor byteLength to remove outdated optimizations
Browse files Browse the repository at this point in the history
The third argument `mustMatch` is now outdated, so remove it.

Fixes: #38536
  • Loading branch information
pd4d10 committed May 5, 2021
1 parent efd70f4 commit 79a836d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 8 additions & 9 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,17 +738,16 @@ function byteLength(string, encoding) {
}

const len = string.length;
const mustMatch = (arguments.length > 2 && arguments[2] === true);
if (!mustMatch && len === 0)
if (len === 0)
return 0;

if (!encoding)
return (mustMatch ? -1 : byteLengthUtf8(string));

const ops = getEncodingOps(encoding);
if (ops === undefined)
return (mustMatch ? -1 : byteLengthUtf8(string));
return ops.byteLength(string);
if (encoding) {
const ops = getEncodingOps(encoding);
if (ops) {
return ops.byteLength(string);
}
}
return byteLengthUtf8(string);
}

Buffer.byteLength = byteLength;
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-buffer-bytelength.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const vm = require('vm');
);
});

assert.strictEqual(Buffer.byteLength('', undefined, true), -1);

assert(ArrayBuffer.isView(new Buffer(10)));
assert(ArrayBuffer.isView(new SlowBuffer(10)));
assert(ArrayBuffer.isView(Buffer.alloc(10)));
Expand Down

0 comments on commit 79a836d

Please sign in to comment.