Skip to content

Commit cff14bc

Browse files
authored
buffer: refactor byteLength to remove outdated optimizations
The third argument `mustMatch` is now outdated, so remove it. Fixes: #38536 PR-URL: #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>
1 parent e520039 commit cff14bc

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

lib/buffer.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -738,17 +738,16 @@ function byteLength(string, encoding) {
738738
}
739739

740740
const len = string.length;
741-
const mustMatch = (arguments.length > 2 && arguments[2] === true);
742-
if (!mustMatch && len === 0)
741+
if (len === 0)
743742
return 0;
744743

745-
if (!encoding)
746-
return (mustMatch ? -1 : byteLengthUtf8(string));
747-
748-
const ops = getEncodingOps(encoding);
749-
if (ops === undefined)
750-
return (mustMatch ? -1 : byteLengthUtf8(string));
751-
return ops.byteLength(string);
744+
if (encoding) {
745+
const ops = getEncodingOps(encoding);
746+
if (ops) {
747+
return ops.byteLength(string);
748+
}
749+
}
750+
return byteLengthUtf8(string);
752751
}
753752

754753
Buffer.byteLength = byteLength;

test/parallel/test-buffer-bytelength.js

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ const vm = require('vm');
2323
);
2424
});
2525

26-
assert.strictEqual(Buffer.byteLength('', undefined, true), -1);
27-
2826
assert(ArrayBuffer.isView(new Buffer(10)));
2927
assert(ArrayBuffer.isView(new SlowBuffer(10)));
3028
assert(ArrayBuffer.isView(Buffer.alloc(10)));

0 commit comments

Comments
 (0)