Skip to content

url: keep the next '=' across pairs in parseParams - #67653

Open
aayushbtw wants to merge 1 commit into
nodejs:mainfrom
aayushbtw:url-searchparams-linear-parse
Open

aayushbtw wants to merge 1 commit into
nodejs:mainfrom
aayushbtw:url-searchparams-linear-parse

Conversation

@aayushbtw

@aayushbtw aayushbtw commented Nov 20, 2026 •

Copy link
Copy Markdown

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. Request#formData() reaches the same loop with an urlencoded body, so a client can block the event loop with one body: a 21 MB body of a& held a server's CPU for over 4 minutes before it was stopped.

This keeps the position of the next = across pairs and searches again only once the parser has passed it.

Timings of the loop copied into a script, median of 5, v26.13.1 arm64:

pairs of a& before after
65,536 94 ms 1.7 ms
262,144 1476 ms 8.3 ms

The copied loop with and without the change returned the same output for 300,000 random strings over a b = & ? + %2 0. The new test covers pairs with no = before and after pairs that have one, empty keys and values, a leading ?, and encoded input. The new test checks that output; it passes before and after the change, since it guards the parse result rather than its speed. The speed is in a new benchmark, benchmark/url/url-searchparams-parse-no-equals.js. On v26.13.1, without the change, pairs=1e5 runs 4.6 ops/s for a against 306 ops/s for a=b.

I have not built Node locally, so I haven't run the full suite or the benchmark against the change; Node's ESLint config (tools/eslint) passes on the three files.

Fixes: #67652
Refs: #65363


Disclosure: I found and fixed this with a coding agent, which also opened this PR and #67652 from my account. I checked the fix against the source, compared the old and new loop on 300,000 random inputs, ran Node's ESLint, and timed the new benchmark on v26.13.1. I'll answer review comments myself.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

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: nodejs#67652
Refs: nodejs#65363
Signed-off-by: Aayush <21987529+aayushbtw@users.noreply.github.com>
@aayushbtw
aayushbtw force-pushed the url-searchparams-linear-parse branch from b77080c to bf418cf Compare November 20, 2026 13:21
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.73%. Comparing base (5884b8e) to head (bf418cf).
⚠️ Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67653      +/-   ##
==========================================
+ Coverage   90.71%   90.73%   +0.01%     
==========================================
  Files         801      801              
  Lines      280906   280911       +5     
  Branches    54249    54249              
==========================================
+ Hits       254832   254892      +60     
+ Misses      16246    16203      -43     
+ Partials     9828     9816      -12     
Files with missing lines Coverage Δ
lib/internal/url.js 93.78% <100.00%> (+0.01%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Renegade334 Renegade334 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                                                                     confidence improvement accuracy (*)    (**)   (***)
url/url-searchparams-parse-no-equals.js n=100 pairs=10 pair='a'              *      9.75 %       ±8.82% ±11.74% ±15.29%
url/url-searchparams-parse-no-equals.js n=100 pairs=10 pair='a=b'                  -2.58 %       ±9.75% ±12.98% ±16.90%
url/url-searchparams-parse-no-equals.js n=100 pairs=100 pair='a'                    4.43 %       ±9.81% ±13.06% ±17.02%
url/url-searchparams-parse-no-equals.js n=100 pairs=100 pair='a=b'                 -0.13 %       ±6.92%  ±9.21% ±11.99%
url/url-searchparams-parse-no-equals.js n=100 pairs=1000 pair='a'          ***     48.15 %       ±9.25% ±12.34% ±16.13%
url/url-searchparams-parse-no-equals.js n=100 pairs=1000 pair='a=b'                -5.37 %       ±5.87%  ±7.81% ±10.16%

@Renegade334 Renegade334 added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Nov 21, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 21, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@Renegade334 Renegade334 added commit-queue PRs queued for automated landing through the Commit Queue. lts-watch-v24.x PRs that may need to be released in v24.x lts-watch-v26.x PRs that may need to be released in v26.x. labels Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:mixed-signals author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. lts-watch-v24.x PRs that may need to be released in v24.x lts-watch-v26.x PRs that may need to be released in v26.x. needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new URLSearchParams(string) is quadratic when the pairs have no =

5 participants