Skip to content

Commit fe96492

Browse files
gurgundayaduh95
authored andcommitted
url: remove array.reduce usage
PR-URL: #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>
1 parent 9b82411 commit fe96492

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,
@@ -280,25 +278,19 @@ class URLSearchParamsIterator {
280278
}
281279
const index = this.#index;
282280
const values = getURLSearchParamsList(this.#target);
283-
const output = ArrayPrototypeReduce(
284-
ArrayPrototypeSlice(values, index),
285-
(prev, cur, i) => {
286-
const key = i % 2 === 0;
287-
if (this.#kind === 'key' && key) {
288-
ArrayPrototypePush(prev, cur);
289-
} else if (this.#kind === 'value' && !key) {
290-
ArrayPrototypePush(prev, cur);
291-
} else if (this.#kind === 'key+value' && !key) {
292-
ArrayPrototypePush(prev, [values[index + i - 1], cur]);
293-
}
294-
return prev;
295-
},
296-
[],
297-
);
298-
const breakLn = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
281+
const output = [];
282+
for (let i = index; i < values.length; i++) {
283+
const isKey = ((i - index) % 2) === 0;
284+
if (this.#kind === 'key') {
285+
if (isKey) ArrayPrototypePush(output, values[i]);
286+
} else if (!isKey) {
287+
ArrayPrototypePush(output, this.#kind === 'value' ? values[i] : [values[i - 1], values[i]]);
288+
}
289+
}
290+
const hasBreak = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
299291
const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts));
300292
let outputStr;
301-
if (breakLn) {
293+
if (hasBreak) {
302294
outputStr = `\n ${ArrayPrototypeJoin(outputStrs, ',\n ')}`;
303295
} else {
304296
outputStr = ` ${ArrayPrototypeJoin(outputStrs, ', ')}`;
@@ -455,11 +447,10 @@ class URLSearchParams {
455447
output,
456448
`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
457449

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

0 commit comments

Comments
 (0)