Skip to content

Commit a2b7d67

Browse files
dinwwwhclaude
andauthored
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

File tree

‎packages/router/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ function expand<T>(node: Node<T>, pending: unknown[], start: number, whole: bool
405405
// first part, a route waits before one, and before literal text like it,
406406
// which keeps the first of two that read the same. A route added whole
407407
// goes after the routes waiting there.
408-
if (segment.charCodeAt(0) === 123) {
408+
if (segment[0] === '{') {
409409
if (!whole && index > start) {
410410
if (index < literals) {
411411
literals = index

0 commit comments

Comments
 (0)