Skip to content

Commit

Permalink
perf: pre allocate array instead of push item (brianc#3250)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nigrosimone authored Jan 17, 2025
1 parent 2de02f0 commit 3c48f22
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
10 changes: 5 additions & 5 deletions packages/pg-native/bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
})
})
}
Expand Down
23 changes: 10 additions & 13 deletions packages/pg-native/lib/build-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/pg/lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down

0 comments on commit 3c48f22

Please sign in to comment.