Skip to content

url: speed up URLPattern - #65364

Open
anonrig wants to merge 1 commit into
nodejs:mainfrom
anonrig:cursor/urlpattern-performance-603e
Open

url: speed up URLPattern#65364
anonrig wants to merge 1 commit into
nodejs:mainfrom
anonrig:cursor/urlpattern-performance-603e

Conversation

@anonrig

@anonrig anonrig commented Aug 18, 2026

Copy link
Copy Markdown
Member

This speeds up WHATWG URLPattern constructor, test(), and exec() without changing observable behavior.

Independent of the new URL() parse PR (#65361) and the URLSearchParams PR (#65363).

What changed

C++ only (src/node_url_pattern.cc):

  • String input: copy one-byte ASCII V8 strings with ValueView + memcpy into a stack buffer and pass a string_view into Ada. Drop the extra std::string copy that BufferValue/Utf8Value::ToString() used to make. Non-ASCII still goes through Utf8Value.
  • Object init: look up the nine interned component keys and assign fields by index. Skip UTF-8-converting each key name and the string-compare chain. Non-string members are still ignored.
  • Regexp provider: construct ASCII patterns/inputs with String::NewFromOneByte and use isolate->GetCurrentContext() instead of Environment::GetCurrent.
  • test / exec: share argument parsing so both paths keep the same WebIDL null/undefined/"null" baseURL behavior.

Ada still owns its parsed state; string_views only live for the duration of the C++ call. ValueView is destroyed before any V8 heap allocation.

Tests

  • test/parallel/test-urlpattern.js, test-urlpattern-types.js, test-urlpattern-invalidthis.js, and test-urlpattern-fast-path.js all pass
  • WPT test/wpt/test-urlpattern.js: 743 passed, 0 unexpected failures

Local benches

Same machine, n=1e5. Rates in ops/s:

Benchmark Before After
parse https://(sub.)?example(.com/)foo 190k 206k
parse { hostname } 269k 277k
parse { pathname, search, hash, baseURL } 243k 260k
parse { pathname: regexp } 237k 254k
test string pattern 2.83M 3.18M
test { hostname } 5.54M 6.20M
test { pathname, search, hash, baseURL } 5.50M 6.10M
test { pathname: regexp } 3.16M 3.21M

Constructor is about 3–9% faster; test() is about 11–12% faster on the non-regexp-heavy cases. Remaining time is Ada parse/match.

Avoid extra UTF-8 copies on constructor, test, and exec; construct
ASCII V8 strings with NewFromOneByte for regexp matching; and assign
URLPatternInit fields by interned key instead of string-comparing
each component name.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
Signed-off-by: Cursor Agent <cursoragent@cursor.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++. needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Aug 18, 2026

@jasnell jasnell 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.

AI agents are not permitted to use Signed-off-by

Comment thread src/node_url_pattern.cc
length);
}
return String::NewFromUtf8(
isolate, view.data(), NewStringType::kNormal, length);

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.

We already have ToV8Value that takes an std::string_view

Comment thread src/node_url_pattern.cc
// and destroy the view before creating any V8 heap objects.
void CopyV8StringToBuffer(Isolate* isolate,
Local<String> str,
MaybeStackBuffer<char>& buffer) {

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.

Project convention is to pass pointers when mutating and to only pass refs as const ref

@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.33333% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.11%. Comparing base (f83e7df) to head (351c14d).
⚠️ Report is 48 commits behind head on main.

Files with missing lines Patch % Lines
src/node_url_pattern.cc 88.33% 3 Missing and 11 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65364      +/-   ##
==========================================
- Coverage   90.31%   90.11%   -0.21%     
==========================================
  Files         751      752       +1     
  Lines      249956   251884    +1928     
  Branches    47204    47346     +142     
==========================================
+ Hits       225745   226979    +1234     
- Misses      15612    16221     +609     
- Partials     8599     8684      +85     
Files with missing lines Coverage Δ
src/node_url_pattern.cc 83.48% <88.33%> (-0.24%) ⬇️

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

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++. 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.

4 participants