lib: avoid .toLowerCase() call in Buffer#write()
Avoid a costly String#toLowerCase() call in Buffer#write() in the common case, i.e., that the string is already lowercase. Reduces the running time of the following benchmark by about 40%: for (var b = Buffer(1), i = 0; i < 25e6; ++i) b.write('x', 'ucs2'); PR-URL: https://github.com/iojs/io.js/pull/1048 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
parent
bbf54a554a
commit
4ddd6406ce
@ -480,47 +480,45 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
|
|||||||
if (length === undefined || length > remaining)
|
if (length === undefined || length > remaining)
|
||||||
length = remaining;
|
length = remaining;
|
||||||
|
|
||||||
encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
|
|
||||||
|
|
||||||
if (string.length > 0 && (length < 0 || offset < 0))
|
if (string.length > 0 && (length < 0 || offset < 0))
|
||||||
throw new RangeError('attempt to write outside buffer bounds');
|
throw new RangeError('attempt to write outside buffer bounds');
|
||||||
|
|
||||||
var ret;
|
if (!encoding)
|
||||||
|
encoding = 'utf8';
|
||||||
|
|
||||||
|
var loweredCase = false;
|
||||||
|
for (;;) {
|
||||||
switch (encoding) {
|
switch (encoding) {
|
||||||
case 'hex':
|
case 'hex':
|
||||||
ret = this.hexWrite(string, offset, length);
|
return this.hexWrite(string, offset, length);
|
||||||
break;
|
|
||||||
|
|
||||||
case 'utf8':
|
case 'utf8':
|
||||||
case 'utf-8':
|
case 'utf-8':
|
||||||
ret = this.utf8Write(string, offset, length);
|
return this.utf8Write(string, offset, length);
|
||||||
break;
|
|
||||||
|
|
||||||
case 'ascii':
|
case 'ascii':
|
||||||
ret = this.asciiWrite(string, offset, length);
|
return this.asciiWrite(string, offset, length);
|
||||||
break;
|
|
||||||
|
|
||||||
case 'binary':
|
case 'binary':
|
||||||
ret = this.binaryWrite(string, offset, length);
|
return this.binaryWrite(string, offset, length);
|
||||||
break;
|
|
||||||
|
|
||||||
case 'base64':
|
case 'base64':
|
||||||
// Warning: maxLength not taken into account in base64Write
|
// Warning: maxLength not taken into account in base64Write
|
||||||
ret = this.base64Write(string, offset, length);
|
return this.base64Write(string, offset, length);
|
||||||
break;
|
|
||||||
|
|
||||||
case 'ucs2':
|
case 'ucs2':
|
||||||
case 'ucs-2':
|
case 'ucs-2':
|
||||||
case 'utf16le':
|
case 'utf16le':
|
||||||
case 'utf-16le':
|
case 'utf-16le':
|
||||||
ret = this.ucs2Write(string, offset, length);
|
return this.ucs2Write(string, offset, length);
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
if (loweredCase)
|
||||||
throw new TypeError('Unknown encoding: ' + encoding);
|
throw new TypeError('Unknown encoding: ' + encoding);
|
||||||
|
encoding = ('' + encoding).toLowerCase();
|
||||||
|
loweredCase = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user