mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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: https://github.com/nodejs/node/issues/67652
Refs: https://github.com/nodejs/node/pull/65363
Signed-off-by: Aayush <21987529+aayushbtw@users.noreply.github.com>
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
423b9df7d6
commit
6264df4160
3 files changed
+53
-1
No files matched your search
@@ -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);
|
||||
}
|
||||
+6
-1
@@ -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 {
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
Reference in new issue
Block a user