Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: pre allocate array instead of push item #3250

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 11 additions & 14 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),
})
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