Skip to content

perf(router): compare a part's first character directly when building the tree - #197

Merged
dinwwwh merged 1 commit into
mainfrom
claude/project-thread-m32ra9
Oct 29, 2026
Merged

dinwwwh merged 1 commit into
mainfrom
claude/project-thread-m32ra9

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Oct 29, 2026

Copy link
Copy Markdown
Member

Requested by dinwwwh · project thread

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

… 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
@dinwwwh dinwwwh self-assigned this Oct 29, 2026
@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Direct first-character compare in expand — segment.charCodeAt(0) === 123 becomes segment[0] === '{' in packages/router/src/index.ts:408. Verified behaviorally identical (empty part gives undefined vs NaN, 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.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 29 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing claude/project-thread-m32ra9 (cb9301f) with main (2b6c549)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@dinwwwh
dinwwwh merged commit a2b7d67 into main Oct 29, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants