diff --git a/benchmark/4e822a1.txt b/benchmark/4e822a1.txt new file mode 100644 index 0000000..ce94b25 --- /dev/null +++ b/benchmark/4e822a1.txt @@ -0,0 +1,17 @@ +benchmark +starting simple-query-parsing +4166 ops/sec - (100/0.024) +8333 ops/sec - (1000/0.12) +10405 ops/sec - (10000/0.961) +10515 ops/sec - (10000/0.951) +10638 ops/sec - (10000/0.94) +10460 ops/sec - (10000/0.956) +starting prepared-statement-parsing +4166 ops/sec - (100/0.024) +8264 ops/sec - (1000/0.121) +7530 ops/sec - (10000/1.328) +8250 ops/sec - (10000/1.212) +8156 ops/sec - (10000/1.226) +8110 ops/sec - (10000/1.233) +done + diff --git a/lib/connection.js b/lib/connection.js index b8ebd79..2bdd07f 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -6,6 +6,8 @@ var util = require('util'); var utils = require(__dirname + '/utils'); var Writer = require('buffer-writer'); +var TEXT_MODE = 0; +var BINARY_MODE = 1; var Connection = function(config) { EventEmitter.call(this); config = config || {}; @@ -19,6 +21,7 @@ var Connection = function(config) { this.writer = new Writer(); this.ssl = config.ssl || false; this._ending = false; + this._mode = TEXT_MODE; }; util.inherits(Connection, EventEmitter); @@ -488,8 +491,15 @@ Connection.prototype.parseField = function() { dataTypeID: this.parseInt32(), dataTypeSize: this.parseInt16(), dataTypeModifier: this.parseInt32(), - format: this.parseInt16() === 0 ? 'text' : 'binary' + format: undefined }; + if(this.parseInt16() === TEXT_MODE) { + this._mode = TEXT_MODE; + field.format = 'text'; + } else { + this._mode = BINARY_MODE; + field.format = 'binary'; + } return field; }; @@ -500,7 +510,11 @@ Connection.prototype.parseD = function(msg) { var length = this.parseInt32(); var value = null; if(length !== -1) { - value = this.readBytes(length); + if(this._mode === TEXT_MODE) { + value = this.readString(length); + } else { + value = this.readBytes(length); + } } fields.push(value); } @@ -569,7 +583,7 @@ Connection.prototype.parseGH = function (msg) { }; Connection.prototype.readChar = function() { - return Buffer([this.buffer[this.offset++]]).toString(this.encoding); + return this.readString(1); }; Connection.prototype.parseInt32 = function() { @@ -594,7 +608,7 @@ Connection.prototype.readBytes = function(length) { Connection.prototype.parseCString = function() { var start = this.offset; - while(this.buffer[this.offset++]) { } + while(this.buffer[this.offset++] !== 0) { } return this.buffer.toString(this.encoding, start, this.offset - 1); };