Skip to content

Commit 5c1dd29

Browse files
committed
increase speed of javascript parser ~5%
1 parent 53b8e3b commit 5c1dd29

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

benchmark/4e822a1.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
benchmark
2+
starting simple-query-parsing
3+
4166 ops/sec - (100/0.024)
4+
8333 ops/sec - (1000/0.12)
5+
10405 ops/sec - (10000/0.961)
6+
10515 ops/sec - (10000/0.951)
7+
10638 ops/sec - (10000/0.94)
8+
10460 ops/sec - (10000/0.956)
9+
starting prepared-statement-parsing
10+
4166 ops/sec - (100/0.024)
11+
8264 ops/sec - (1000/0.121)
12+
7530 ops/sec - (10000/1.328)
13+
8250 ops/sec - (10000/1.212)
14+
8156 ops/sec - (10000/1.226)
15+
8110 ops/sec - (10000/1.233)
16+
done
17+

lib/connection.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var Readable = require('stream').Readable;
88
var utils = require(__dirname + '/utils');
99
var Writer = require('buffer-writer');
1010

11+
var TEXT_MODE = 0;
12+
var BINARY_MODE = 1;
1113
var Connection = function(config) {
1214
EventEmitter.call(this);
1315
config = config || {};
@@ -21,6 +23,7 @@ var Connection = function(config) {
2123
this.writer = new Writer();
2224
this.ssl = config.ssl || false;
2325
this._ending = false;
26+
this._mode = TEXT_MODE;
2427
};
2528

2629
util.inherits(Connection, EventEmitter);
@@ -491,8 +494,15 @@ Connection.prototype.parseField = function() {
491494
dataTypeID: this.parseInt32(),
492495
dataTypeSize: this.parseInt16(),
493496
dataTypeModifier: this.parseInt32(),
494-
format: this.parseInt16() === 0 ? 'text' : 'binary'
497+
format: undefined
495498
};
499+
if(this.parseInt16() === TEXT_MODE) {
500+
this._mode = TEXT_MODE;
501+
field.format = 'text';
502+
} else {
503+
this._mode = BINARY_MODE;
504+
field.format = 'binary';
505+
}
496506
return field;
497507
};
498508

@@ -503,7 +513,11 @@ Connection.prototype.parseD = function(msg) {
503513
var length = this.parseInt32();
504514
var value = null;
505515
if(length !== -1) {
506-
value = this.readBytes(length);
516+
if(this._mode === TEXT_MODE) {
517+
value = this.readString(length);
518+
} else {
519+
value = this.readBytes(length);
520+
}
507521
}
508522
fields.push(value);
509523
}
@@ -572,7 +586,7 @@ Connection.prototype.parseGH = function (msg) {
572586
};
573587

574588
Connection.prototype.readChar = function() {
575-
return Buffer([this.buffer[this.offset++]]).toString(this.encoding);
589+
return this.readString(1);
576590
};
577591

578592
Connection.prototype.parseInt32 = function() {
@@ -597,7 +611,7 @@ Connection.prototype.readBytes = function(length) {
597611

598612
Connection.prototype.parseCString = function() {
599613
var start = this.offset;
600-
while(this.buffer[this.offset++]) { }
614+
while(this.buffer[this.offset++] !== 0) { }
601615
return this.buffer.toString(this.encoding, start, this.offset - 1);
602616
};
603617

0 commit comments

Comments
 (0)