From 751e7410d9742c2ca2164b536a922bdcd45fe22a Mon Sep 17 00:00:00 2001 From: francesco Date: Mon, 10 Feb 2025 23:26:33 +0100 Subject: [PATCH] perf(utils): fast prepareValue (#3370) * perf(utils): fast prepareValue This PR add a performance improvements at prepare Value for non-object by skipping useless condition * fix: lint * fix: case of undefined * fix: review --- packages/pg/lib/utils.js | 41 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index 40b8f6ed..3bcc4e52 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -49,27 +49,28 @@ var prepareValue = function (val, seen) { if (val == null) { return null } - if (val instanceof Buffer) { - return val - } - if (ArrayBuffer.isView(val)) { - var buf = Buffer.from(val.buffer, val.byteOffset, val.byteLength) - if (buf.length === val.byteLength) { - return buf - } - return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params - } - if (val instanceof Date) { - if (defaults.parseInputDatesAsUTC) { - return dateToStringUTC(val) - } else { - return dateToString(val) - } - } - if (Array.isArray(val)) { - return arrayString(val) - } if (typeof val === 'object') { + if (val instanceof Buffer) { + return val + } + if (ArrayBuffer.isView(val)) { + var buf = Buffer.from(val.buffer, val.byteOffset, val.byteLength) + if (buf.length === val.byteLength) { + return buf + } + return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params + } + if (val instanceof Date) { + if (defaults.parseInputDatesAsUTC) { + return dateToStringUTC(val) + } else { + return dateToString(val) + } + } + if (Array.isArray(val)) { + return arrayString(val) + } + return prepareObject(val, seen) } return val.toString()