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..bf128e7038406f 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 (!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)); 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 ')} }`;