Repository navigation
Commit a2b7d67
perf(router): compare a part's first character directly when building the tree (#197)
<!-- claude-projects-attribution: {"github_login":"dinwwwh"} -->
_Requested by **dinwwwh** · [project
thread](https://claude.ai/code/project/chan_01UcKMz91tMarPV1kWLVgLYE?thread=cmsg_01UcKMz91tMarPV1kWLVgLYE6swgSUM9SyULmBVdRjY2t7)_
## Summary
`expand` checks every part it walks for a leading `{`. In the
interpreter, `segment.charCodeAt(0) === 123` loads `charCodeAt` from the
string prototype (a LoadIC) and calls it, while `segment[0] === '{'` is
one keyed load and a compare against an internalized one-character
string. An empty part gives `undefined`, which is not `'{'`, as before.
It also reads better.
I tried the same in `toSegment` and `trimTrailingSlash` too: `toSegment`
came out worse on REST cold+10 (+0.8%), since `text[text.length - 1]`
costs a `length` load more than it saves, and `trimTrailingSlash` is 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:
| Stage | main | PR | Change |
| --- | --- | --- | --- |
| REST, 3 requests | 3.577M | 3.535M | -1.2% |
| REST, 3 requests on 3 methods | 5.199M | 5.115M | -1.6% |
| REST, 10 requests | 6.159M | 6.074M | -1.4% |
| 100 real nouns, 10 requests | 6.128M | 6.040M | -1.4% |
| RPC, 10 requests | 420k | 420k | same |
| Router import | 2.179M | 2.178M | same |
- Size (esbuild `--minify`, gzip -9): 5071 B to 5060 B minified, 2105 B
to 2108 B gzip.
- Steady state: unchanged, since `expand` only runs while building the
tree.
## Checks
- `pnpm run check` green; router tests 73/73 with 100% coverage.
- All fuzzers at 3 seeds: 0 diffs.
- Stryker: 68 survivors, the same as main; the `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](https://claude.ai/code/session_01VVaw7Sutbw6xMTMyDFeNfw)_
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2b6c549 commit a2b7d67
1 file changed
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
405 | 405 | | |
406 | 406 | | |
407 | 407 | | |
408 | | - | |
| 408 | + | |
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
| |||
0 commit comments