Repository navigation
perf(router): compare a part's first character directly when building the tree - #197
Conversation
… the tree
`segment[0] === '{'` is one keyed load and a compare in the interpreter,
where `segment.charCodeAt(0) === 123` loads the method and calls it. It is
also easier to read.
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! |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Direct first-character compare in
expand—segment.charCodeAt(0) === 123becomessegment[0] === '{'inpackages/router/src/index.ts:408. Verified behaviorally identical (empty part givesundefinedvsNaN, neither matching), and it matches the surrounding comment about parts "starting with{once decoded".
Checked locally on the PR head: tsc -p packages/router/tsconfig.json --noEmit and uncheck on the file pass (oxlint, oxfmt, tsc), and vitest run packages/router is 73/73 green. The perf measurement and the deliberate decision to leave toSegment/trimTrailingSlash unchanged are well documented in the PR body.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
Merging this PR will not alter performance1 warning
Comparing Footnotes
|

Requested by dinwwwh · project thread
Summary
expandchecks every part it walks for a leading{. In the interpreter,segment.charCodeAt(0) === 123loadscharCodeAtfrom the string prototype (a LoadIC) and calls it, whilesegment[0] === '{'is one keyed load and a compare against an internalized one-character string. An empty part givesundefined, which is not'{', as before. It also reads better.I tried the same in
toSegmentandtrimTrailingSlashtoo:toSegmentcame out worse on REST cold+10 (+0.8%), sincetext[text.length - 1]costs alengthload more than it saves, andtrimTrailingSlashis on the steady path, so both stay as they are.Measurements
Callgrind instructions,
--single-threaded, hash seeds 1 and 2 (averaged), main vs this PR:--minify, gzip -9): 5071 B to 5060 B minified, 2105 B to 2108 B gzip.expandonly runs while building the tree.Checks
pnpm run checkgreen; router tests 73/73 with 100% coverage.if (true)survivor of this check (every part waiting or parsing as a possible param, which only does more work) moves to the new line.Generated by Claude Code