mirror of
https://github.com/nodejs/node.git
synced 2025-12-27 23:41:14 +00:00
url: remove array.reduce usage
PR-URL: https://github.com/nodejs/node/pull/60748 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
parent
482b191727
commit
7fe8085727
39
benchmark/url/url-searchparams-inspect.js
Normal file
39
benchmark/url/url-searchparams-inspect.js
Normal file
@ -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);
|
||||
}
|
||||
@ -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 ')} }`;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user