Repository navigation
refactor(router): compare tail and prefix segments in place - #199
Conversation
`matchesSegment` was a one-line function shared by `matchesCatchAll` and the prefix matcher. Comparing in place spares V8 a function to set up at import and to compile on the first request that reaches a catch-all or a prefix with a param. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VVaw7Sutbw6xMTMyDFeNfw
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
matchesSegmentinlined at both call sites — the module-privateexpected === undefined ? segment !== '' : segment === expectedhelper is removed and its negation written directly increatePrefixMatcher's closure and inmatchesCatchAll's tail loop. The inlined ternaries are the exact De Morgan negation of the helper, so behavior is unchanged; I confirmed both sites and ran the router suite (73 tests pass).- Comment added documenting that
undefinedstands for a param requiring a non-empty segment.
The refactor is mechanical and provably behavior-preserving: !matchesSegment expands to expected === undefined ? segment === '' : segment !== expected on both branches. matchesSegment was module-private with only these two callers (impact scan found no tracked references), and no public export changed.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

Requested by dinwwwh · project thread
What
matchesSegmentwas a one-line function used in two places:matchesCatchAll, for the segments after a catch-all param, and the closure thatcreatePrefixMatcherreturns. Both now do the comparison in place, with a comment thatundefinedstands for a param, which needs a non-empty segment.Why
Each top-level function costs V8 about 15k instructions to set up at import, and about as much again to compile when it is first called (see #198). This removes one function from every import. It also removes one compile from the first request that reaches a catch-all, or a prefix with a param.
Measurements
Instruction counts from callgrind,
node --single-threaded, averaged over hash seeds 1 and 2:%Cold+3 on the REST set reaches no catch-all, so it changes only by the import saving.
Checks
Generated by Claude Code