perf: cut query-string parsing cost on the request hot path#53
Merged
Conversation
queryparams runs on every request. Two pieces of per-request work were
avoidable:
1. `search.replace(/\[\]=/g, '=')` ran a regex over every query string even
when no `a[]=` array notation was present. Now gated on `indexOf('[]=')`.
2. `name.split(/[.[\]]+/).filter(Boolean)` allocated an array per parameter,
every request, solely to guard against `__proto__`/`prototype`/`constructor`
keys. The split now runs only for names containing `proto`/`constructor`
(a superset of all dangerous keys), so normal params skip it entirely.
Behavior is identical — verified across repeated/array params and all
prototype-pollution key forms (new tests in router-coverage).
Measured (Node 22, 3M iters):
typical ?q=node&page=2&limit=20 : 489 -> 312 ns/op (-36%)
array ?id[]=1&id[]=2&tag=x : 595 -> 428 ns/op (-28%)
no-query /api/users : 10.7 ns/op (unchanged)
Suite: 71 passing, queryparams.js at 100% line coverage.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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
lib/utils/queryparams.jsruns on every request (and again per nested-router level), so it's squarely on the hot path. Two pieces of per-request work were avoidable, and removing them speeds up query parsing ~36% with no behavior change and no API change.1. Unconditional array-notation regex
new URLSearchParams(search.replace(/\[\]=/g, '='))scanned/allocated a regex replacement over every query string, even when noa[]=array notation existed. Now gated on a cheapsearch.indexOf('[]=')check.2. Per-parameter allocation for the prototype-pollution guard
name.split(/[.[\]]+/).filter(Boolean)allocated a throwaway array for every parameter on every request, solely to check for__proto__/prototype/constructorsegments. Those are vanishingly rare in real traffic. The split now runs only when the name containsprotoorconstructor(a substring superset of all three dangerous keys), so normal parameters skip the allocation entirely.Behavior is identical
The dangerous-key filtering and array/repeat grouping are unchanged. Verified by two new tests in
tests/router-coverage.test.js:[]=array notation grouping__proto__,prototype,user.constructor,a[__proto__]) are stripped, query object stays null-prototype, andObject.prototypeis not polluted.I also diffed old-vs-new output across 11 URLs (including every dangerous form) — 0 differences.
Measured (Node 22, 3M iterations, warmed)
?q=node&page=2&limit=20?id[]=1&id[]=2&tag=x/api/users)Validation
lib/utils/queryparams.js: 100% line coverage.standardlint clean.Out of scope (deferred)
A second optimization — nested routers re-parse the (identical) query string at every level — was identified but deliberately deferred: it requires adding per-request state and a correctness decision about query immutability across the middleware chain. Happy to follow up in a separate PR if wanted.
🤖 Generated with Claude Code