Skip to content

url: speed up setters, search params, and URLPattern - #66188

Open
anonrig wants to merge 2 commits into
nodejs:mainfrom
anonrig:url-hot-paths
Open

anonrig wants to merge 2 commits into
nodejs:mainfrom
anonrig:url-hot-paths

Conversation

@anonrig

@anonrig anonrig commented Sep 21, 2026

Copy link
Copy Markdown
Member

Why

URL setters always reparsed the href, including when the assigned value was already the serialized component. URLSearchParams built from a record walked Reflect.ownKeys, and every get / has converted the name again. URL.canParse and URLPattern.test took the slow string callback for ASCII input.

What

A setter skips ada when the new value matches the component already stored on the href, and skips the slice when the lengths differ. An empty host or hostname, and a lone ? or #, still go through ada, because those writes are not idempotent. Record URLSearchParams uses Object.keys when there are no symbol keys. Repeated get, getAll, and has of the same string skip a second USVString conversion. URL.canParse and URLPattern.prototype.test use the one-byte fast API; two-byte strings stay on the slow path.

benchmark/compare.js --runs 5 on macOS arm64 Release, against the parent commit built with the same toolchain. Stars in that tool mark a significant difference; the rows below are significant. Assigning the current pathname on its own went from 3.9M to 106M ops/s. Setting a different pathname was flat (+3%).

Benchmark Change
whatwg-url-properties href, with base +150%
whatwg-url-properties password, with base +118%
whatwg-url-properties port, with base +113%
whatwg-url-properties protocol, with base +110%
whatwg-url-properties username, with base +108%
whatwg-url-properties pathname, with base +76%
whatwg-url-properties host, with base +71%
whatwg-url-properties hash, with base +64%
whatwg-url-properties search, with base +62%
whatwg-url-properties hostname, with base +55%
whatwg-url-canParse long +54%
whatwg-url-canParse short +45%
whatwg-url-canParse ws +29%
whatwg-url-canParse file +28%
whatwg-url-canParse javascript +28%
whatwg-url-canParse auth +19%
url-searchparams-creation object, noencode +68%
url-searchparams-read has one +88%
url-searchparams-read get one +79%
urlpattern-test exact pathname/search/hash +32%
urlpattern-test hostname +32%

test/parallel/test-whatwg-url*, test-urlpattern*, test-url*, and WPT url / urlpattern pass. A closed-source coding agent assisted with the implementation.

Skip the ada reparse when a setter is given the value already stored
on the URL, and skip the component slice when the lengths differ.
Empty host and a lone "?" or "#" still go through ada, because those
assignments are not idempotent.

URLSearchParams records with no symbol keys use Object.keys. Repeated
get, getAll, and has of the same string skip a second USVString
conversion. URL.canParse and URLPattern.prototype.test use the
one-byte fast API.

Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Sep 21, 2026
Debug builds count url.canParse and url.canParse.withBase. The
one-byte fast path renamed those keys, so the shared debug jobs
saw a count of 0.

Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
@codecov

codecov Bot commented Sep 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.64078% with 66 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.28%. Comparing base (67a4416) to head (078b443).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/node_url_pattern.cc 49.35% 35 Missing and 4 partials ⚠️
lib/internal/url.js 92.27% 15 Missing and 1 partial ⚠️
src/node_url.cc 56.00% 10 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66188      +/-   ##
==========================================
- Coverage   90.28%   90.28%   -0.01%     
==========================================
  Files         790      790              
  Lines      272044   272231     +187     
  Branches    51949    51990      +41     
==========================================
+ Hits       245625   245782     +157     
- Misses      16915    16966      +51     
+ Partials     9504     9483      -21     
Files with missing lines Coverage Δ
src/node_url.h 100.00% <ø> (ø)
src/node_url_pattern.h 0.00% <ø> (ø)
src/node_url.cc 77.80% <56.00%> (-1.15%) ⬇️
lib/internal/url.js 92.83% <92.27%> (-0.44%) ⬇️
src/node_url_pattern.cc 77.25% <49.35%> (-6.28%) ⬇️

... and 20 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.

@jasnell

jasnell commented Sep 21, 2026

Copy link
Copy Markdown
Member

The test coverage gaps in this should be looked at

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants