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
46 changes: 46 additions & 0 deletions benchmark/url/url-searchparams-getAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const common = require('../common');
const assert = require('node:assert');

const bench = common.createBenchmark(main, {
n: [1e6],
size: [4, 32, 256],
matches: ['none', 'one', 'half', 'all'],
mode: ['read', 'construct'],
});

function main({ n, size, matches, mode }) {
const params = new URLSearchParams();
const expected = [];
for (let index = 0; index < size; index++) {
const match = matches === 'all' ||
(matches === 'half' && index % 2 === 0) ||
(matches === 'one' && index === size - 1);
const value = `${index}`;
params.append(match ? 'target' : `key${index}`, value);
if (match) expected.push(value);
}

const query = params.toString();
let checksum = 0;
let result;
bench.start();
if (mode === 'read') {
for (let iteration = 0; iteration < n; iteration++) {
result = params.getAll('target');
checksum += result.length;
if (result.length !== 0) checksum += result[result.length - 1].length;
}
} else {
for (let iteration = 0; iteration < n; iteration++) {
result = new URLSearchParams(query).getAll('target');
checksum += result.length;
if (result.length !== 0) checksum += result[result.length - 1].length;
}
}
bench.end(n);
assert.deepStrictEqual(result, expected);
assert.strictEqual(checksum, n * (expected.length +
(expected.length === 0 ? 0 : expected[expected.length - 1].length)));
}
2 changes: 1 addition & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ class URLSearchParams {
const { length } = list;
for (let i = 0; i < length; i += 2) {
if (list[i] === name) {
ArrayPrototypePush(values, list[i + 1]);
values[values.length] = list[i + 1];
}
}
return values;
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-getall-results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

require('../common');
const assert = require('node:assert');

for (const size of [0, 1, 4, 32, 256]) {
const params = new URLSearchParams();
const expected = [];
for (let index = 0; index < size; index++) {
params.append('other', `${index}`);
const value = index % 2 === 0 ? '' : `value${index}`;
params.append('target', value);
expected.push(value);
}

const result = params.getAll('target');
assert.deepStrictEqual(result, expected);
result.push('extra');
result[0] = 'changed';
assert.deepStrictEqual(params.getAll('target'), expected);
params.append('target', 'last');
assert.deepStrictEqual(params.getAll('target'), [...expected, 'last']);
assert.deepStrictEqual(params.getAll('missing'), []);
}
Loading