Skip to content

Commit b33c266

Browse files
committed
increase speed of javascript parser ~5%
1 parent f16eaa8 commit b33c266

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
@@ -6,6 +6,8 @@ var util = require('util');
66
var utils = require(__dirname + '/utils');
77
var Writer = require('buffer-writer');
88

9+
var TEXT_MODE = 0;
10+
var BINARY_MODE = 1;
911
var Connection = function(config) {
1012
EventEmitter.call(this);
1113
config = config || {};
@@ -19,6 +21,7 @@ var Connection = function(config) {
1921
this.writer = new Writer();
2022
this.ssl = config.ssl || false;
2123
this._ending = false;
24+
this._mode = TEXT_MODE;
2225
};
2326

2427
util.inherits(Connection, EventEmitter);
@@ -488,8 +491,15 @@ Connection.prototype.parseField = function() {
488491
dataTypeID: this.parseInt32(),
489492
dataTypeSize: this.parseInt16(),
490493
dataTypeModifier: this.parseInt32(),
491-
format: this.parseInt16() === 0 ? 'text' : 'binary'
494+
format: undefined
492495
};
496+
if(this.parseInt16() === TEXT_MODE) {
497+
this._mode = TEXT_MODE;
498+
field.format = 'text';
499+
} else {
500+
this._mode = BINARY_MODE;
501+
field.format = 'binary';
502+
}
493503
return field;
494504
};
495505

@@ -500,7 +510,11 @@ Connection.prototype.parseD = function(msg) {
500510
var length = this.parseInt32();
501511
var value = null;
502512
if(length !== -1) {
503-
value = this.readBytes(length);
513+
if(this._mode === TEXT_MODE) {
514+
value = this.readString(length);
515+
} else {
516+
value = this.readBytes(length);
517+
}
504518
}
505519
fields.push(value);
506520
}
@@ -569,7 +583,7 @@ Connection.prototype.parseGH = function (msg) {
569583
};
570584

571585
Connection.prototype.readChar = function() {
572-
return Buffer([this.buffer[this.offset++]]).toString(this.encoding);
586+
return this.readString(1);
573587
};
574588

575589
Connection.prototype.parseInt32 = function() {
@@ -594,7 +608,7 @@ Connection.prototype.readBytes = function(length) {
594608

595609
Connection.prototype.parseCString = function() {
596610
var start = this.offset;
597-
while(this.buffer[this.offset++]) { }
611+
while(this.buffer[this.offset++] !== 0) { }
598612
return this.buffer.toString(this.encoding, start, this.offset - 1);
599613
};
600614

0 commit comments

Comments
 (0)