Skip to content

Commit 1a9f3f9

Browse files
committed
buffer: optimize byteLength for common encodings
1 parent 298ff4f commit 1a9f3f9

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/buffer.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,20 @@ function byteLength(string, encoding) {
771771
if (len === 0)
772772
return 0;
773773

774-
if (encoding) {
775-
const ops = getEncodingOps(encoding);
776-
if (ops) {
777-
return ops.byteLength(string);
778-
}
774+
if (!encoding || encoding === 'utf8') {
775+
return byteLengthUtf8(string);
776+
}
777+
778+
if (encoding === 'ascii') {
779+
return len;
780+
}
781+
782+
const ops = getEncodingOps(encoding);
783+
if (ops === undefined) {
784+
throw new ERR_UNKNOWN_ENCODING(encoding);
779785
}
780-
return byteLengthUtf8(string);
786+
787+
return ops.byteLength(string);
781788
}
782789

783790
Buffer.byteLength = byteLength;

0 commit comments

Comments
 (0)