Skip to content

Commit

Permalink
buffer: improve Buffer.byteLength(string, encoding)
Browse files Browse the repository at this point in the history
When string is empty, it will running into binding also.
It make the performance is wasted.
  • Loading branch information
JacksonTian committed Apr 16, 2015
1 parent 10e31ba commit 16ea356
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,27 @@ function byteLength(string, encoding) {
if (typeof(string) !== 'string')
string = String(string);

switch (encoding) {
case 'ascii':
case 'binary':
case 'raw':
return string.length;
if (string.length > 0) {
switch (encoding) {
case 'ascii':
case 'binary':
case 'raw':
return string.length;

case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return string.length * 2;
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return string.length * 2;

case 'hex':
return string.length >>> 1;
}
case 'hex':
return string.length >>> 1;
}

return binding.byteLength(string, encoding);
return binding.byteLength(string, encoding);
} else {
return 0;
}
}

Buffer.byteLength = byteLength;
Expand Down

0 comments on commit 16ea356

Please sign in to comment.