From cd0ec1a813e0eec7d21e36bd9c5e91d40c69cafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sun, 16 Nov 2025 13:25:06 +0100 Subject: [PATCH 1/2] url: remove array.reduce usage --- benchmark/url/url-searchparams-inspect.js | 39 +++++++++++++++++++++++ lib/internal/url.js | 39 +++++++++-------------- 2 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 benchmark/url/url-searchparams-inspect.js diff --git a/benchmark/url/url-searchparams-inspect.js b/benchmark/url/url-searchparams-inspect.js new file mode 100644 index 00000000000000..ceccb4f488a1e6 --- /dev/null +++ b/benchmark/url/url-searchparams-inspect.js @@ -0,0 +1,39 @@ +'use strict'; +const common = require('../common.js'); +const { inspect } = require('util'); + +const bench = common.createBenchmark(main, { + variant: ['empty', 'small', 'medium', 'large'], + kind: ['params', 'iterator-keys', 'iterator-values', 'iterator-entries'], + n: [1e5], +}); + +function makeParams(size) { + const u = new URLSearchParams(); + for (let i = 0; i < size; i++) { + u.append('k' + i, 'v' + i); + } + return u; +} + +function main({ variant, kind, n }) { + const sizes = { empty: 0, small: 3, medium: 16, large: 128 }; + const size = sizes[variant]; + const params = makeParams(size); + let target; + if (kind === 'params') { + target = params; + } else if (kind === 'iterator-keys') { + target = params.keys(); + } else if (kind === 'iterator-values') { + target = params.values(); + } else { + target = params.entries(); + } + + bench.start(); + for (let i = 0; i < n; i++) { + inspect(target, { showHidden: false, depth: 2 }); + } + bench.end(n); +} diff --git a/lib/internal/url.js b/lib/internal/url.js index a1473fdac8aba3..5cdeb2abbf8a96 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -6,8 +6,6 @@ const { ArrayPrototypeJoin, ArrayPrototypeMap, ArrayPrototypePush, - ArrayPrototypeReduce, - ArrayPrototypeSlice, Boolean, Int8Array, IteratorPrototype, @@ -281,25 +279,19 @@ class URLSearchParamsIterator { } const index = this.#index; const values = getURLSearchParamsList(this.#target); - const output = ArrayPrototypeReduce( - ArrayPrototypeSlice(values, index), - (prev, cur, i) => { - const key = i % 2 === 0; - if (this.#kind === 'key' && key) { - ArrayPrototypePush(prev, cur); - } else if (this.#kind === 'value' && !key) { - ArrayPrototypePush(prev, cur); - } else if (this.#kind === 'key+value' && !key) { - ArrayPrototypePush(prev, [values[index + i - 1], cur]); - } - return prev; - }, - [], - ); - const breakLn = StringPrototypeIncludes(inspect(output, innerOpts), '\n'); + const output = []; + for (let i = index; i < values.length; i++) { + const isKey = ((i - index) % 2) === 0; + if (this.#kind === 'key') { + if (isKey) ArrayPrototypePush(output, values[i]); + } else if (this.#kind === 'value') { + if (!isKey) ArrayPrototypePush(output, values[i]); + } else if (!isKey) ArrayPrototypePush(output, [values[i - 1], values[i]]); + } + const hasBreak = StringPrototypeIncludes(inspect(output, innerOpts), '\n'); const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts)); let outputStr; - if (breakLn) { + if (hasBreak) { outputStr = `\n ${ArrayPrototypeJoin(outputStrs, ',\n ')}`; } else { outputStr = ` ${ArrayPrototypeJoin(outputStrs, ', ')}`; @@ -456,11 +448,10 @@ class URLSearchParams { output, `${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`); - const length = ArrayPrototypeReduce( - output, - (prev, cur) => prev + removeColors(cur).length + separator.length, - -separator.length, - ); + let length = -separator.length; + for (let i = 0; i < output.length; i++) { + length += removeColors(output[i]).length + separator.length; + } if (length > ctx.breakLength) { return `${this.constructor.name} {\n` + ` ${ArrayPrototypeJoin(output, ',\n ')} }`; From 7e3b8710c45e81a67c9723705617b813812a84b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sun, 16 Nov 2025 15:30:05 +0100 Subject: [PATCH 2/2] apply suggestion Co-authored-by: Antoine du Hamel --- lib/internal/url.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 5cdeb2abbf8a96..bf128e7038406f 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -284,9 +284,9 @@ class URLSearchParamsIterator { const isKey = ((i - index) % 2) === 0; if (this.#kind === 'key') { if (isKey) ArrayPrototypePush(output, values[i]); - } else if (this.#kind === 'value') { - if (!isKey) ArrayPrototypePush(output, values[i]); - } else if (!isKey) ArrayPrototypePush(output, [values[i - 1], values[i]]); + } else if (!isKey) { + ArrayPrototypePush(output, this.#kind === 'value' ? values[i] : [values[i - 1], values[i]]); + } } const hasBreak = StringPrototypeIncludes(inspect(output, innerOpts), '\n'); const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts));