perf(headers): O(1) case-insensitive lookup in HeadersAdapter#91559
Open
benfavre wants to merge 1 commit intovercel:canaryfrom
Open
perf(headers): O(1) case-insensitive lookup in HeadersAdapter#91559benfavre wants to merge 1 commit intovercel:canaryfrom
benfavre wants to merge 1 commit intovercel:canaryfrom
Conversation
…sAdapter The HeadersAdapter Proxy handler was calling `Object.keys(headers).find(o => o.toLowerCase() === lowercased)` on every `get`, `set`, `has`, and `deleteProperty` trap. This is O(n) per access where n is the number of headers. Headers are accessed many times during every SSR request (cookies, content-type, accept, user-agent, authorization, custom headers, etc.), making this a significant hot-path bottleneck. With a typical request carrying 10-20 headers and each being accessed multiple times during rendering, the cumulative cost of repeated linear scans is substantial. This commit replaces the linear scan with a `Map<string, string>` that maps lowercase keys to their original casing, turning every access into an O(1) `Map.get()`. The map is: - Built once in the constructor from the initial headers - Kept in sync by `set` (adds new entries) and `deleteProperty` (removes) - Handles out-of-band mutations on the underlying IncomingHttpHeaders object by falling back to a linear scan on cache miss and caching the result for subsequent accesses Inspired by TanStack Start's findings on avoiding O(n) operations in SSR hot paths as a key factor in achieving higher throughput. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
1 similar comment
Collaborator
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
9 tasks
Contributor
Author
Test Verification
All tests run on the |
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.
Summary
Object.keys().find()with O(1)Map.get()in theHeadersAdapterProxy traps (get,set,has,deleteProperty)Map<lowercase, originalKey>once in the constructor from initial headersset(adds entries) anddeleteProperty(removes entries)IncomingHttpHeadersobject by falling back to a linear scan on cache miss and caching the resultMotivation
The
HeadersAdapteris used on every SSR request to wrap Node.jsIncomingHttpHeaders. Every call to.get(),.has(),.set(), ordeleteon the adapter triggers a Proxy trap that does:This is O(n) per access where n = number of headers. A typical HTTP request carries 10-20 headers, and during SSR rendering headers are accessed many times (cookies, content-type, accept, authorization, user-agent, x-forwarded-for, custom headers, etc.). The cumulative cost of repeated linear scans over every header key on every access adds up in high-throughput SSR scenarios.
This is the same class of optimization highlighted in the TanStack Start blog post about 5x SSR throughput — avoiding O(n) operations in hot paths is one of the most impactful patterns for SSR performance.
The fix pre-computes a
Map<string, string>(lowercase -> original key) and usesMap.get()for O(1) lookups. The map is kept consistent throughset/deleteoperations and handles the edge case where external code mutates theIncomingHttpHeadersobject directly (tested in the existing test suite).Test plan
HeadersAdapterunit tests pass, including the "mutated out of band" case that validates the fallback path🤖 Generated with Claude Code