Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions benchmark/url/url-searchparams-inspect.js
Original file line number Diff line number Diff line change
@@ -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);
}
39 changes: 15 additions & 24 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeReduce,
ArrayPrototypeSlice,
Boolean,
Int8Array,
IteratorPrototype,
Expand Down Expand Up @@ -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, ', ')}`;
Expand Down Expand Up @@ -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 ')} }`;
Expand Down
Loading