From bf418cfc1669508ebab6d54095988eaeaf856ac4 Mon Sep 17 00:00:00 2001 From: Aayush <21987529+aayushbtw@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:13:12 +0400 Subject: [PATCH] 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> --- .../url/url-searchparams-parse-no-equals.js | 20 ++++++++++++++ lib/internal/url.js | 7 ++++- ...whatwg-url-searchparams-parse-no-equals.js | 27 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 benchmark/url/url-searchparams-parse-no-equals.js create mode 100644 test/parallel/test-whatwg-url-searchparams-parse-no-equals.js 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 000000000000..51675d51ca42 --- /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 95a43b1c53a7..682c502e4cb3 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 000000000000..a67a2e7863b8 --- /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'); +}