mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
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
This commit is contained in:
parent
2de02f0a63
commit
3c48f22b22
@ -19,7 +19,7 @@ var warmup = function (fn, cb) {
|
||||
var native = Native()
|
||||
native.connectSync()
|
||||
|
||||
var queryText = 'SELECT generate_series(0, 1000)'
|
||||
var queryText = 'SELECT generate_series(0, 1000) as X, generate_series(0, 1000) as Y, generate_series(0, 1000) as Z'
|
||||
var client = new pg.Client()
|
||||
client.connect(function () {
|
||||
var pure = function (cb) {
|
||||
@ -36,12 +36,12 @@ client.connect(function () {
|
||||
}
|
||||
|
||||
var run = function () {
|
||||
var start = Date.now()
|
||||
console.time('pure')
|
||||
warmup(pure, function () {
|
||||
console.log('pure done', Date.now() - start)
|
||||
start = Date.now()
|
||||
console.timeEnd('pure')
|
||||
console.time('native')
|
||||
warmup(nativeQuery, function () {
|
||||
console.log('native done', Date.now() - start)
|
||||
console.timeEnd('native')
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@ -18,46 +18,43 @@ class Result {
|
||||
|
||||
consumeFields(pq) {
|
||||
const nfields = pq.nfields()
|
||||
this.fields = new Array(nfields)
|
||||
for (var x = 0; x < nfields; x++) {
|
||||
this.fields.push({
|
||||
this.fields[x] = {
|
||||
name: pq.fname(x),
|
||||
dataTypeID: pq.ftype(x),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
consumeRows(pq) {
|
||||
const tupleCount = pq.ntuples()
|
||||
this.rows = new Array(tupleCount)
|
||||
for (var i = 0; i < tupleCount; i++) {
|
||||
const row = this._arrayMode ? this.consumeRowAsArray(pq, i) : this.consumeRowAsObject(pq, i)
|
||||
this.rows.push(row)
|
||||
this.rows[i] = this._arrayMode ? this.consumeRowAsArray(pq, i) : this.consumeRowAsObject(pq, i)
|
||||
}
|
||||
}
|
||||
|
||||
consumeRowAsObject(pq, rowIndex) {
|
||||
const row = {}
|
||||
for (var j = 0; j < this.fields.length; j++) {
|
||||
const value = this.readValue(pq, rowIndex, j)
|
||||
row[this.fields[j].name] = value
|
||||
row[this.fields[j].name] = this.readValue(pq, rowIndex, j)
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
consumeRowAsArray(pq, rowIndex) {
|
||||
const row = []
|
||||
const row = new Array(this.fields.length)
|
||||
for (var j = 0; j < this.fields.length; j++) {
|
||||
const value = this.readValue(pq, rowIndex, j)
|
||||
row.push(value)
|
||||
row[j] = this.readValue(pq, rowIndex, j)
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
readValue(pq, rowIndex, colIndex) {
|
||||
var rawValue = pq.getvalue(rowIndex, colIndex)
|
||||
if (rawValue === '') {
|
||||
if (pq.getisnull(rowIndex, colIndex)) {
|
||||
return null
|
||||
}
|
||||
if (rawValue === '' && pq.getisnull(rowIndex, colIndex)) {
|
||||
return null
|
||||
}
|
||||
const dataTypeId = this.fields[colIndex].dataTypeID
|
||||
return this._types.getTypeParser(dataTypeId)(rawValue)
|
||||
|
||||
@ -37,7 +37,7 @@ class Result {
|
||||
if (match) {
|
||||
this.command = match[1]
|
||||
if (match[3]) {
|
||||
// COMMMAND OID ROWS
|
||||
// COMMAND OID ROWS
|
||||
this.oid = parseInt(match[2], 10)
|
||||
this.rowCount = parseInt(match[3], 10)
|
||||
} else if (match[2]) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user