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
20 changes: 20 additions & 0 deletions benchmark/url/url-searchparams-parse-no-equals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common.js');
const assert = require('node:assert');

const bench = common.createBenchmark(main, {
pair: ['a', 'a=b'],
pairs: [1e3, 1e5],
n: [100],
});

function main({ pair, pairs, n }) {
const input = `${pair}&`.repeat(pairs);
let params;
bench.start();
for (let i = 0; i < n; i++) {
params = new URLSearchParams(input);
}
bench.end(n);
assert.strictEqual(params.size, pairs);
}
7 changes: 6 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1366,13 +1366,18 @@ function parseParams(qs) {
const out = [];
// Native indexOf/slice/push outperform primordials on this tight loop.
const encoded = qs.indexOf('+', i) !== -1 || qs.indexOf('%', i) !== -1;
// The next '=' at or after `i`, kept across pairs so that a run of pairs
// with no '=' does not rescan the rest of the string for each pair.
let eq = -2;
while (i < len) {
let amp = qs.indexOf('&', i);
if (amp === -1) {
amp = len;
}
if (amp !== i) {
const eq = qs.indexOf('=', i);
if (eq !== -1 && eq < i) {
eq = qs.indexOf('=', i);
}
if (eq === -1 || eq > amp) {
out.push(encoded ? decodeFormComponent(qs, i, amp) : qs.slice(i, amp), '');
} else {
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-parse-no-equals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

// Pairs with no '=' mixed with pairs that have one.

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

const cases = [
['a&b&c=d', [['a', ''], ['b', ''], ['c', 'd']]],
['a=b&c&d&e=f&g', [['a', 'b'], ['c', ''], ['d', ''], ['e', 'f'], ['g', '']]],
['a&&b=c=d&&e', [['a', ''], ['b', 'c=d'], ['e', '']]],
['=&a&=b', [['', ''], ['a', ''], ['', 'b']]],
['?a&b=', [['a', ''], ['b', '']]],
['a+b&c%20d=e+f&g', [['a b', ''], ['c d', 'e f'], ['g', '']]],
[`${'a&'.repeat(50)}x=y`, [...Array(50).fill(['a', '']), ['x', 'y']]],
];

for (const [input, expected] of cases) {
assert.deepStrictEqual([...new URLSearchParams(input)], expected);
}

{
const pairs = 1 << 18;
const params = new URLSearchParams(`${'a&'.repeat(pairs)}x=y`);
assert.strictEqual(params.size, pairs + 1);
assert.strictEqual(params.get('x'), 'y');
}
Loading