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: nodejs#38536

PR-URL: nodejs#38545
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
pd4d10 authored and xtx1130 committed Apr 25, 2022
1 parent dbef236 commit 6d8335e
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 6d8335e

Please sign in to comment.