perf: improve Headers class performance - #5699
Open
anonrig wants to merge 2 commits into
Open
Conversation
Speed up the WHATWG Headers hot path:
- Combine ByteString conversion, token validation, and lowercasing
into a single pass, with a fast path for common lowercase names.
- Store header values as strings instead of {name, value} objects.
- Copy an existing Headers list without re-validating every entry.
- Skip ToString in the ByteString converter when the value is already
a string.
Signed-off-by: Cursor Agent <cursoragent@cursor.com>
Keep the Headers hot-path changes, but put the fetch spec comments back and drop the unrelated ByteString converter change. Signed-off-by: Cursor Agent <cursoragent@cursor.com>
cursor Bot
pushed a commit
to anonrig/node
that referenced
this pull request
Aug 18, 2026
Add WHATWG Headers unit tests and a fetch/headers benchmark so Node can track the API after the implementation change lands in undici. Refs: nodejs/undici#5699 Co-authored-by: Yagiz Nizipli <yagiz@nizipli.com> Signed-off-by: Cursor Agent <cursoragent@cursor.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5699 +/- ##
==========================================
- Coverage 93.47% 93.46% -0.01%
==========================================
Files 110 110
Lines 38846 39119 +273
==========================================
+ Hits 36310 36562 +252
- Misses 2536 2557 +21 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
mcollina
requested changes
Aug 18, 2026
mcollina
left a comment
Member
There was a problem hiding this comment.
Adding the benchmarks somewhere will be good.
Also, lint is failing.
@KhafraDev I'm generically ok with these improvements. However it's quite substantial in the code changes. Take a look.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This relates to...
N/A
Rationale
Headersis on the fetch / Request / Response hot path. The current implementation pays for WebIDL conversion, token validation, andtoLowerCase()as separate passes, and stores every entry as a{ name, value }object.Changes
content-type,accept,authorization, …) onget/has/delete.Mapinstead of{ name, value }objects. Original casing is kept in a side table only when it differs from the lowercase name.Headersinstance by cloning the list instead of re-validating every entry.lib/web/fetch/headers.js.No intended behavioral change. Guard handling,
set-cookie/getSetCookie(), iteration order, and original-name casing on the wire are preserved.Features
N/A
Bug Fixes
N/A
Breaking Changes and Deprecations
N/A
Benchmark
Measured with the source
Headersclass (Node v22.14.0, 200k iterations, paired old vs new):get(custom name)get(content-type)hassetnew Headers(headers)new Headers()appendStatus
Tested locally:
All 70 tests passed, including the 4000-iteration
toSortedArrayfast/slow paths and the fetch cloning cases.