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,
|
ArrayPrototypeJoin,
|
||||||
ArrayPrototypeMap,
|
ArrayPrototypeMap,
|
||||||
ArrayPrototypePush,
|
ArrayPrototypePush,
|
||||||
ArrayPrototypeReduce,
|
|
||||||
ArrayPrototypeSlice,
|
|
||||||
Boolean,
|
Boolean,
|
||||||
Int8Array,
|
Int8Array,
|
||||||
IteratorPrototype,
|
IteratorPrototype,
|
||||||
@ -281,25 +279,19 @@ class URLSearchParamsIterator {
|
|||||||
}
|
}
|
||||||
const index = this.#index;
|
const index = this.#index;
|
||||||
const values = getURLSearchParamsList(this.#target);
|
const values = getURLSearchParamsList(this.#target);
|
||||||
const output = ArrayPrototypeReduce(
|
const output = [];
|
||||||
ArrayPrototypeSlice(values, index),
|
for (let i = index; i < values.length; i++) {
|
||||||
(prev, cur, i) => {
|
const isKey = ((i - index) % 2) === 0;
|
||||||
const key = i % 2 === 0;
|
if (this.#kind === 'key') {
|
||||||
if (this.#kind === 'key' && key) {
|
if (isKey) ArrayPrototypePush(output, values[i]);
|
||||||
ArrayPrototypePush(prev, cur);
|
} else if (!isKey) {
|
||||||
} else if (this.#kind === 'value' && !key) {
|
ArrayPrototypePush(output, this.#kind === 'value' ? values[i] : [values[i - 1], values[i]]);
|
||||||
ArrayPrototypePush(prev, cur);
|
}
|
||||||
} else if (this.#kind === 'key+value' && !key) {
|
}
|
||||||
ArrayPrototypePush(prev, [values[index + i - 1], cur]);
|
const hasBreak = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
|
||||||
}
|
|
||||||
return prev;
|
|
||||||
},
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
const breakLn = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
|
|
||||||
const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts));
|
const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts));
|
||||||
let outputStr;
|
let outputStr;
|
||||||
if (breakLn) {
|
if (hasBreak) {
|
||||||
outputStr = `\n ${ArrayPrototypeJoin(outputStrs, ',\n ')}`;
|
outputStr = `\n ${ArrayPrototypeJoin(outputStrs, ',\n ')}`;
|
||||||
} else {
|
} else {
|
||||||
outputStr = ` ${ArrayPrototypeJoin(outputStrs, ', ')}`;
|
outputStr = ` ${ArrayPrototypeJoin(outputStrs, ', ')}`;
|
||||||
@ -456,11 +448,10 @@ class URLSearchParams {
|
|||||||
output,
|
output,
|
||||||
`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
|
`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
|
||||||
|
|
||||||
const length = ArrayPrototypeReduce(
|
let length = -separator.length;
|
||||||
output,
|
for (let i = 0; i < output.length; i++) {
|
||||||
(prev, cur) => prev + removeColors(cur).length + separator.length,
|
length += removeColors(output[i]).length + separator.length;
|
||||||
-separator.length,
|
}
|
||||||
);
|
|
||||||
if (length > ctx.breakLength) {
|
if (length > ctx.breakLength) {
|
||||||
return `${this.constructor.name} {\n` +
|
return `${this.constructor.name} {\n` +
|
||||||
` ${ArrayPrototypeJoin(output, ',\n ')} }`;
|
` ${ArrayPrototypeJoin(output, ',\n ')} }`;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user