You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
some microbenchmark updates, slight speed improvement
This commit is contained in:
@@ -24,6 +24,17 @@ util.inherits(FasterReplyParser, events.EventEmitter);
|
||||
|
||||
exports.Parser = FasterReplyParser;
|
||||
|
||||
// Buffer.toString() is quite slow for small strings
|
||||
function small_toString(buf, start, end) {
|
||||
var tmp = "", i;
|
||||
|
||||
for (i = start; i < end; i++) {
|
||||
tmp += String.fromCharCode(buf[i]);
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
FasterReplyParser.prototype._parseResult = function (type) {
|
||||
var start, end, offset, packetHeader;
|
||||
|
||||
@@ -38,7 +49,11 @@ FasterReplyParser.prototype._parseResult = function (type) {
|
||||
if (this.options.return_buffers) {
|
||||
return this._buffer.slice(start, end);
|
||||
} else {
|
||||
return this._buffer.toString(this._encoding, start, end);
|
||||
if (end - start < 65536) { // completely arbitrary
|
||||
return small_toString(this._buffer, start, end);
|
||||
} else {
|
||||
return this._buffer.toString(this._encoding, start, end);
|
||||
}
|
||||
}
|
||||
} else if (type === 58) { // :
|
||||
// up to the delimiter
|
||||
@@ -49,7 +64,7 @@ FasterReplyParser.prototype._parseResult = function (type) {
|
||||
this._offset = end + 2;
|
||||
|
||||
// return the coerced numeric value
|
||||
return +this._buffer.toString(this._encoding, start, end);
|
||||
return +small_toString(this._buffer, start, end);
|
||||
} else if (type === 36) { // $
|
||||
// set a rewind point, as the packet could be larger than the
|
||||
// buffer in memory
|
||||
@@ -185,8 +200,8 @@ FasterReplyParser.prototype.append = function(newBuffer) {
|
||||
|
||||
FasterReplyParser.prototype.parseHeader = function () {
|
||||
var end = this._packetEndOffset(),
|
||||
value = this._buffer.toString(this._encoding, this._offset, end - 1);
|
||||
|
||||
value = small_toString(this._buffer, this._offset, end - 1);
|
||||
|
||||
this._offset = end + 1;
|
||||
|
||||
return value;
|
||||
|
Reference in New Issue
Block a user