Skip to content

Commit 6264df4

Browse files
aayushbtwaduh95
authored andcommitted
url: keep the next '=' across pairs in parseParams
parseParams searched for '=' from the start of every pair. In a run of pairs with no '=' after them, each search scanned to the end of the string, so new URLSearchParams('a&'.repeat(n)) took quadratic time: about 1.6 s for 262,144 pairs against 7.5 ms for 'a=b&'. Request's formData() reaches the same loop with an urlencoded body. Keep the position of the next '=' and search again only once the parser has passed it. Fixes: #67652 Refs: #65363 Signed-off-by: Aayush <21987529+aayushbtw@users.noreply.github.com> PR-URL: #67653 Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 423b9df commit 6264df4

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const assert = require('node:assert');
4+
5+
const bench = common.createBenchmark(main, {
6+
pair: ['a', 'a=b'],
7+
pairs: [1e3, 1e5],
8+
n: [100],
9+
});
10+
11+
function main({ pair, pairs, n }) {
12+
const input = `${pair}&`.repeat(pairs);
13+
let params;
14+
bench.start();
15+
for (let i = 0; i < n; i++) {
16+
params = new URLSearchParams(input);
17+
}
18+
bench.end(n);
19+
assert.strictEqual(params.size, pairs);
20+
}

‎lib/internal/url.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,13 +1366,18 @@ function parseParams(qs) {
13661366
const out = [];
13671367
// Native indexOf/slice/push outperform primordials on this tight loop.
13681368
const encoded = qs.indexOf('+', i) !== -1 || qs.indexOf('%', i) !== -1;
1369+
// The next '=' at or after `i`, kept across pairs so that a run of pairs
1370+
// with no '=' does not rescan the rest of the string for each pair.
1371+
let eq = -2;
13691372
while (i < len) {
13701373
let amp = qs.indexOf('&', i);
13711374
if (amp === -1) {
13721375
amp = len;
13731376
}
13741377
if (amp !== i) {
1375-
const eq = qs.indexOf('=', i);
1378+
if (eq !== -1 && eq < i) {
1379+
eq = qs.indexOf('=', i);
1380+
}
13761381
if (eq === -1 || eq > amp) {
13771382
out.push(encoded ? decodeFormComponent(qs, i, amp) : qs.slice(i, amp), '');
13781383
} else {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// Pairs with no '=' mixed with pairs that have one.
4+
5+
require('../common');
6+
const assert = require('node:assert');
7+
8+
const cases = [
9+
['a&b&c=d', [['a', ''], ['b', ''], ['c', 'd']]],
10+
['a=b&c&d&e=f&g', [['a', 'b'], ['c', ''], ['d', ''], ['e', 'f'], ['g', '']]],
11+
['a&&b=c=d&&e', [['a', ''], ['b', 'c=d'], ['e', '']]],
12+
['=&a&=b', [['', ''], ['a', ''], ['', 'b']]],
13+
['?a&b=', [['a', ''], ['b', '']]],
14+
['a+b&c%20d=e+f&g', [['a b', ''], ['c d', 'e f'], ['g', '']]],
15+
[`${'a&'.repeat(50)}x=y`, [...Array(50).fill(['a', '']), ['x', 'y']]],
16+
];
17+
18+
for (const [input, expected] of cases) {
19+
assert.deepStrictEqual([...new URLSearchParams(input)], expected);
20+
}
21+
22+
{
23+
const pairs = 1 << 18;
24+
const params = new URLSearchParams(`${'a&'.repeat(pairs)}x=y`);
25+
assert.strictEqual(params.size, pairs + 1);
26+
assert.strictEqual(params.get('x'), 'y');
27+
}

0 commit comments

Comments
 (0)