Commit 4f6a5618258 for nodejs
commit 4f6a5618258ae743bce1f4c25de43ba428d70898
Author: Aayush <21987529+aayushbtw@users.noreply.github.com>
Date: Sun Nov 22 17:24:44 2026 +0400
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>
diff --git a/benchmark/url/url-searchparams-parse-no-equals.js b/benchmark/url/url-searchparams-parse-no-equals.js
new file mode 100644
index 00000000000..51675d51ca4
--- /dev/null
+++ b/benchmark/url/url-searchparams-parse-no-equals.js
@@ -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);
+}
diff --git a/lib/internal/url.js b/lib/internal/url.js
index 95a43b1c53a..682c502e4cb 100644
--- a/lib/internal/url.js
+++ b/lib/internal/url.js
@@ -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 {
diff --git a/test/parallel/test-whatwg-url-searchparams-parse-no-equals.js b/test/parallel/test-whatwg-url-searchparams-parse-no-equals.js
new file mode 100644
index 00000000000..a67a2e7863b
--- /dev/null
+++ b/test/parallel/test-whatwg-url-searchparams-parse-no-equals.js
@@ -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');
+}