From 3a08e853f3af892a1de2f969df291b862168ed60 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sun, 22 Nov 2026 11:08:53 +0100 Subject: [PATCH] perf(pg): skip per-query config copy and unused array-mode row object Every query paid for two allocations it did not need: - The Query and NativeQuery constructors copied the caller's config with Object.getOwnPropertyDescriptors and Object.defineProperties, so that normalizeQueryConfig could write values and callback onto the copy without touching the caller's object (#3720). The constructors now read the config as given and let values and a callback passed as arguments take precedence, with the same precedence as normalizeQueryConfig. The caller's config is still never written to, and getters and prototype properties are read as before. normalizeQueryConfig itself is unchanged. - Result.addFields built _prebuiltEmptyResultObject, an object keyed by column name, for every result set. Only object rows use it, so rowMode 'array' now skips it. --- packages/pg/lib/native/query.js | 14 +++- packages/pg/lib/query.js | 18 +++-- packages/pg/lib/result.js | 9 ++- packages/pg/test/unit/query-config-tests.js | 76 +++++++++++++++++++++ packages/pg/test/unit/result-tests.js | 28 ++++++++ 5 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 packages/pg/test/unit/query-config-tests.js diff --git a/packages/pg/lib/native/query.js b/packages/pg/lib/native/query.js index 39dfcc20b..014112685 100644 --- a/packages/pg/lib/native/query.js +++ b/packages/pg/lib/native/query.js @@ -6,12 +6,20 @@ const utils = require('../utils') const NativeQuery = (module.exports = function (config, values, callback) { EventEmitter.call(this) - config = utils.normalizeQueryConfig(config, values, callback) + // Read the config as given instead of normalizing a copy of it. Values or a + // callback passed as arguments win, as in utils.normalizeQueryConfig. + if (typeof config === 'string') { + config = { text: config } + } + if (typeof values === 'function') { + callback = callback || values + values = undefined + } this.text = config.text - this.values = config.values + this.values = values || config.values this.name = config.name this.queryMode = config.queryMode - this.callback = config.callback + this.callback = callback || config.callback this.state = 'new' this._arrayMode = config.rowMode === 'array' diff --git a/packages/pg/lib/query.js b/packages/pg/lib/query.js index 3b93cb5ba..a3d73e8ee 100644 --- a/packages/pg/lib/query.js +++ b/packages/pg/lib/query.js @@ -9,10 +9,18 @@ class Query extends EventEmitter { constructor(config, values, callback) { super() - config = utils.normalizeQueryConfig(config, values, callback) + // Read the config as given instead of normalizing a copy of it. Values or a + // callback passed as arguments win, as in utils.normalizeQueryConfig. + if (typeof config === 'string') { + config = { text: config } + } + if (typeof values === 'function') { + callback = callback || values + values = undefined + } this.text = config.text - this.values = config.values + this.values = values || config.values this.rows = config.rows this.types = config.types this.name = config.name @@ -20,10 +28,10 @@ class Query extends EventEmitter { this.binary = config.binary // use unique portal name each time this.portal = config.portal || '' - this.callback = config.callback + this.callback = callback || config.callback this._rowMode = config.rowMode - if (process.domain && config.callback) { - this.callback = process.domain.bind(config.callback) + if (process.domain && this.callback) { + this.callback = process.domain.bind(this.callback) } this._result = new Result(this._rowMode, this.types) diff --git a/packages/pg/lib/result.js b/packages/pg/lib/result.js index 329fbf9fc..20428f351 100644 --- a/packages/pg/lib/result.js +++ b/packages/pg/lib/result.js @@ -89,11 +89,14 @@ class Result { this._parsers = new Array(fieldDescriptions.length) } - const row = Object.create(null) + // only object rows start from the prebuilt object; array rows don't need it + const row = this.rowAsArray ? null : Object.create(null) for (let i = 0; i < fieldDescriptions.length; i++) { const desc = fieldDescriptions[i] - row[desc.name] = null + if (row !== null) { + row[desc.name] = null + } if (this._types) { this._parsers[i] = this._types.getTypeParser(desc.dataTypeID, desc.format || 'text') @@ -102,7 +105,7 @@ class Result { } } - this._prebuiltEmptyResultObject = { ...row } + this._prebuiltEmptyResultObject = row === null ? null : { ...row } } } diff --git a/packages/pg/test/unit/query-config-tests.js b/packages/pg/test/unit/query-config-tests.js new file mode 100644 index 000000000..1b0fed8eb --- /dev/null +++ b/packages/pg/test/unit/query-config-tests.js @@ -0,0 +1,76 @@ +'use strict' +const helper = require('./test-helper') +const assert = require('assert') +const suite = new helper.Suite() +const test = suite.test.bind(suite) + +const Query = require('../../lib/query') +const NativeQuery = require('../../lib/native/query') + +for (const [label, QueryClass] of [ + ['Query', Query], + ['NativeQuery', NativeQuery], +]) { + test(`${label} reads text, values and callback like normalizeQueryConfig`, function () { + const callback = function () {} + const other = function () {} + let query + + query = new QueryClass('TEXT') + assert.equal(query.text, 'TEXT') + assert.equal(query.values, undefined) + assert.equal(query.callback, undefined) + + query = new QueryClass('TEXT', [10], callback) + assert.equal(query.text, 'TEXT') + assert.deepEqual(query.values, [10]) + assert.equal(query.callback, callback) + + query = new QueryClass({ text: 'TEXT', values: [10] }) + assert.deepEqual(query.values, [10]) + + query = new QueryClass({ text: 'TEXT', values: [10] }, [20]) + assert.deepEqual(query.values, [20]) + + query = new QueryClass({ text: 'TEXT', values: [10] }, callback) + assert.deepEqual(query.values, [10]) + assert.equal(query.callback, callback) + + query = new QueryClass({ text: 'TEXT', callback: other }) + assert.equal(query.callback, other) + + query = new QueryClass({ text: 'TEXT', callback: other }, callback) + assert.equal(query.callback, callback) + + query = new QueryClass({ text: 'TEXT' }, other, callback) + assert.equal(query.values, undefined) + assert.equal(query.callback, callback) + }) + + test(`${label} does not copy or change the caller's config`, function () { + const callback = function () {} + const config = Object.freeze({ text: 'TEXT', rowMode: 'array' }) + + const query = new QueryClass(config, [10], callback) + + assert.deepEqual(config, { text: 'TEXT', rowMode: 'array' }) + assert.deepEqual(query.values, [10]) + assert.equal(query.callback, callback) + }) + + test(`${label} reads config properties through getters and prototypes`, function () { + class QueryConfig { + constructor() { + this._text = 'TEXT' + } + + get text() { + return this._text + } + } + + const query = new QueryClass(new QueryConfig(), [10]) + assert.equal(query.text, 'TEXT') + assert.deepEqual(query.values, [10]) + }) +} diff --git a/packages/pg/test/unit/result-tests.js b/packages/pg/test/unit/result-tests.js index 5135723ed..440cbe950 100644 --- a/packages/pg/test/unit/result-tests.js +++ b/packages/pg/test/unit/result-tests.js @@ -109,3 +109,31 @@ test('multiple dangerous column names handled safely', function () { assert.strictEqual(row['__defineGetter__'], 'd') assert.strictEqual(row.id, 1) }) + +test('array rows do not build the prebuilt row object', function () { + const result = new Result('array') + result.addFields([ + { name: 'id', dataTypeID: 23, format: 'text' }, + { name: 'name', dataTypeID: 25, format: 'text' }, + ]) + + assert.strictEqual(result._prebuiltEmptyResultObject, null) + assert.deepStrictEqual(result.parseRow(['1', 'a']), [1, 'a']) + assert.deepStrictEqual(result.parseRow([null, 'b']), [null, 'b']) + + // a later result set replaces the parsers + result.addFields([{ name: 'ok', dataTypeID: 16, format: 'text' }]) + assert.strictEqual(result._prebuiltEmptyResultObject, null) + assert.deepStrictEqual(result.parseRow(['t']), [true]) +}) + +test('object rows still start from the prebuilt row object', function () { + const result = new Result() + result.addFields([ + { name: 'id', dataTypeID: 23, format: 'text' }, + { name: 'name', dataTypeID: 25, format: 'text' }, + ]) + + assert.deepStrictEqual(result._prebuiltEmptyResultObject, { id: null, name: null }) + assert.deepStrictEqual(result.parseRow(['1', null]), { id: 1, name: null }) +})