Skip to content

Commit 7fe8085

Browse files
authored
url: remove array.reduce usage
PR-URL: #60748 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Jordan Harband <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 482b191 commit 7fe8085

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { inspect } = require('util');
4+
5+
const bench = common.createBenchmark(main, {
6+
variant: ['empty', 'small', 'medium', 'large'],
7+
kind: ['params', 'iterator-keys', 'iterator-values', 'iterator-entries'],
8+
n: [1e5],
9+
});
10+
11+
function makeParams(size) {
12+
const u = new URLSearchParams();
13+
for (let i = 0; i < size; i++) {
14+
u.append('k' + i, 'v' + i);
15+
}
16+
return u;
17+
}
18+
19+
function main({ variant, kind, n }) {
20+
const sizes = { empty: 0, small: 3, medium: 16, large: 128 };
21+
const size = sizes[variant];
22+
const params = makeParams(size);
23+
let target;
24+
if (kind === 'params') {
25+
target = params;
26+
} else if (kind === 'iterator-keys') {
27+
target = params.keys();
28+
} else if (kind === 'iterator-values') {
29+
target = params.values();
30+
} else {
31+
target = params.entries();
32+
}
33+
34+
bench.start();
35+
for (let i = 0; i < n; i++) {
36+
inspect(target, { showHidden: false, depth: 2 });
37+
}
38+
bench.end(n);
39+
}

lib/internal/url.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const {
66
ArrayPrototypeJoin,
77
ArrayPrototypeMap,
88
ArrayPrototypePush,
9-
ArrayPrototypeReduce,
10-
ArrayPrototypeSlice,
119
Boolean,
1210
Int8Array,
1311
IteratorPrototype,
@@ -281,25 +279,19 @@ class URLSearchParamsIterator {
281279
}
282280
const index = this.#index;
283281
const values = getURLSearchParamsList(this.#target);
284-
const output = ArrayPrototypeReduce(
285-
ArrayPrototypeSlice(values, index),
286-
(prev, cur, i) => {
287-
const key = i % 2 === 0;
288-
if (this.#kind === 'key' && key) {
289-
ArrayPrototypePush(prev, cur);
290-
} else if (this.#kind === 'value' && !key) {
291-
ArrayPrototypePush(prev, cur);
292-
} else if (this.#kind === 'key+value' && !key) {
293-
ArrayPrototypePush(prev, [values[index + i - 1], cur]);
294-
}
295-
return prev;
296-
},
297-
[],
298-
);
299-
const breakLn = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
282+
const output = [];
283+
for (let i = index; i < values.length; i++) {
284+
const isKey = ((i - index) % 2) === 0;
285+
if (this.#kind === 'key') {
286+
if (isKey) ArrayPrototypePush(output, values[i]);
287+
} else if (!isKey) {
288+
ArrayPrototypePush(output, this.#kind === 'value' ? values[i] : [values[i - 1], values[i]]);
289+
}
290+
}
291+
const hasBreak = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
300292
const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts));
301293
let outputStr;
302-
if (breakLn) {
294+
if (hasBreak) {
303295
outputStr = `\n ${ArrayPrototypeJoin(outputStrs, ',\n ')}`;
304296
} else {
305297
outputStr = ` ${ArrayPrototypeJoin(outputStrs, ', ')}`;
@@ -456,11 +448,10 @@ class URLSearchParams {
456448
output,
457449
`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
458450

459-
const length = ArrayPrototypeReduce(
460-
output,
461-
(prev, cur) => prev + removeColors(cur).length + separator.length,
462-
-separator.length,
463-
);
451+
let length = -separator.length;
452+
for (let i = 0; i < output.length; i++) {
453+
length += removeColors(output[i]).length + separator.length;
454+
}
464455
if (length > ctx.breakLength) {
465456
return `${this.constructor.name} {\n` +
466457
` ${ArrayPrototypeJoin(output, ',\n ')} }`;

0 commit comments

Comments
 (0)