From 8d0f2e72393d112093ec2284b20456485e2016b7 Mon Sep 17 00:00:00 2001 From: Jerry Sievert Date: Sun, 8 Jul 2012 08:32:33 -0700 Subject: [PATCH] some microbenchmark updates, slight speed improvement --- lib/parser/javascript.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/parser/javascript.js b/lib/parser/javascript.js index 01c66fe339..2fe310b8da 100644 --- a/lib/parser/javascript.js +++ b/lib/parser/javascript.js @@ -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;