Skip to content

Commit 3c48f22

Browse files
authored
perf: pre allocate array instead of push item (#3250)
* fix: typo * perf: pre allocate array instead of push item * perf: refractoring missing push * perf: avoid useless varible declaration * perf: short control flow * fix: lint * more precise bench * fix: lint
1 parent 2de02f0 commit 3c48f22

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

packages/pg-native/bench/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var warmup = function (fn, cb) {
1919
var native = Native()
2020
native.connectSync()
2121

22-
var queryText = 'SELECT generate_series(0, 1000)'
22+
var queryText = 'SELECT generate_series(0, 1000) as X, generate_series(0, 1000) as Y, generate_series(0, 1000) as Z'
2323
var client = new pg.Client()
2424
client.connect(function () {
2525
var pure = function (cb) {
@@ -36,12 +36,12 @@ client.connect(function () {
3636
}
3737

3838
var run = function () {
39-
var start = Date.now()
39+
console.time('pure')
4040
warmup(pure, function () {
41-
console.log('pure done', Date.now() - start)
42-
start = Date.now()
41+
console.timeEnd('pure')
42+
console.time('native')
4343
warmup(nativeQuery, function () {
44-
console.log('native done', Date.now() - start)
44+
console.timeEnd('native')
4545
})
4646
})
4747
}

packages/pg-native/lib/build-result.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,43 @@ class Result {
1818

1919
consumeFields(pq) {
2020
const nfields = pq.nfields()
21+
this.fields = new Array(nfields)
2122
for (var x = 0; x < nfields; x++) {
22-
this.fields.push({
23+
this.fields[x] = {
2324
name: pq.fname(x),
2425
dataTypeID: pq.ftype(x),
25-
})
26+
}
2627
}
2728
}
2829

2930
consumeRows(pq) {
3031
const tupleCount = pq.ntuples()
32+
this.rows = new Array(tupleCount)
3133
for (var i = 0; i < tupleCount; i++) {
32-
const row = this._arrayMode ? this.consumeRowAsArray(pq, i) : this.consumeRowAsObject(pq, i)
33-
this.rows.push(row)
34+
this.rows[i] = this._arrayMode ? this.consumeRowAsArray(pq, i) : this.consumeRowAsObject(pq, i)
3435
}
3536
}
3637

3738
consumeRowAsObject(pq, rowIndex) {
3839
const row = {}
3940
for (var j = 0; j < this.fields.length; j++) {
40-
const value = this.readValue(pq, rowIndex, j)
41-
row[this.fields[j].name] = value
41+
row[this.fields[j].name] = this.readValue(pq, rowIndex, j)
4242
}
4343
return row
4444
}
4545

4646
consumeRowAsArray(pq, rowIndex) {
47-
const row = []
47+
const row = new Array(this.fields.length)
4848
for (var j = 0; j < this.fields.length; j++) {
49-
const value = this.readValue(pq, rowIndex, j)
50-
row.push(value)
49+
row[j] = this.readValue(pq, rowIndex, j)
5150
}
5251
return row
5352
}
5453

5554
readValue(pq, rowIndex, colIndex) {
5655
var rawValue = pq.getvalue(rowIndex, colIndex)
57-
if (rawValue === '') {
58-
if (pq.getisnull(rowIndex, colIndex)) {
59-
return null
60-
}
56+
if (rawValue === '' && pq.getisnull(rowIndex, colIndex)) {
57+
return null
6158
}
6259
const dataTypeId = this.fields[colIndex].dataTypeID
6360
return this._types.getTypeParser(dataTypeId)(rawValue)

packages/pg/lib/result.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Result {
3737
if (match) {
3838
this.command = match[1]
3939
if (match[3]) {
40-
// COMMMAND OID ROWS
40+
// COMMAND OID ROWS
4141
this.oid = parseInt(match[2], 10)
4242
this.rowCount = parseInt(match[3], 10)
4343
} else if (match[2]) {

0 commit comments

Comments
 (0)