Commit 3e0d4ff
perf: cut query-string parsing cost on the request hot path (#53)
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>1 parent 11ea6e1 commit 3e0d4ff
2 files changed
Lines changed: 33 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
33 | 36 | | |
34 | 37 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
42 | 49 | | |
43 | 50 | | |
44 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
312 | 329 | | |
313 | 330 | | |
314 | 331 | | |
| |||
0 commit comments