mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
buffer: optimize byteLength for common encodings
PR-URL: https://github.com/nodejs/node/pull/54342 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
4e6befd60e
commit
38ad892ccf
@ -782,13 +782,22 @@ function byteLength(string, encoding) {
|
||||
if (len === 0)
|
||||
return 0;
|
||||
|
||||
if (encoding) {
|
||||
const ops = getEncodingOps(encoding);
|
||||
if (ops) {
|
||||
return ops.byteLength(string);
|
||||
}
|
||||
if (!encoding || encoding === 'utf8') {
|
||||
return byteLengthUtf8(string);
|
||||
}
|
||||
return byteLengthUtf8(string);
|
||||
|
||||
if (encoding === 'ascii') {
|
||||
return len;
|
||||
}
|
||||
|
||||
const ops = getEncodingOps(encoding);
|
||||
if (ops === undefined) {
|
||||
// TODO (ronag): Makes more sense to throw here.
|
||||
// throw new ERR_UNKNOWN_ENCODING(encoding);
|
||||
return byteLengthUtf8(string);
|
||||
}
|
||||
|
||||
return ops.byteLength(string);
|
||||
}
|
||||
|
||||
Buffer.byteLength = byteLength;
|
||||
|
Loading…
Reference in New Issue
Block a user