Skip to content

perf(router-core): hoist regex literals evaluated per built location - #8425

Open
schiller-manuel wants to merge 1 commit into
mainfrom
codex/hoist-hot-regex
Open

schiller-manuel wants to merge 1 commit into
mainfrom
codex/hoist-hot-regex

Conversation

@schiller-manuel

@schiller-manuel schiller-manuel commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

🎯 Changes

A regex literal creates a new RegExp object every time it is evaluated
(measured: 56 B per evaluation, 0 once hoisted). decodePath and
encodePathLikeUrl run on every buildLocation, cleanPath on every href of a
basepath router, encodeParam on every splat and trimPathRight on every
trailing-slash path; parseLocation's fast-path test runs per navigation.
Their literals are now module constants. trimPathLeft (construction only)
and the cold branches of getUrlScheme are left as they were.

Allocation per buildLocation (sampling heap profiler over 160k builds against
the production dist, Node 24.8; on top of the previous commit):

relative destination 1309 -> 1197 B (-112)
search updater 1085 -> 969 B (-116)
static destination 955 -> 786 B (-169)

CPU (tests/build-location.bench.ts, two rounds, hz): within noise,
directionally +1-2%: search updater 41.6k/44.2k -> 43.5k/43.2k, relative
29.8k/30.2k -> 29.6k/30.3k, masked 20.5k/20.9k -> 20.8k/20.6k.

Bundle: react-router.minimal +18 gzip (derived from the composed measurements
86077 with, 86059 without these hoists).

Verification on main

  • router-core test:unit (3252 passed), test:types, test:eslint
  • Allocation numbers: sampling heap profiler over 160k buildLocation calls against the production dist/esm (56 B per regex-literal evaluation measured in isolation, 0 once hoisted); CPU numbers from the benchmark in test(router-core): add a Link-like buildLocation benchmark #8423.

Relationship to other PRs

Independent. Benchmark: #8423.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Performance

    • Improved URL path processing efficiency during location parsing, encoding, and decoding.
    • Optimized route matching and comparison operations.
    • Existing URL matching and encoding behavior remains unchanged.
  • Bug Fixes

    • Improved handling of undefined values during route comparisons for more consistent matching behavior.
  • Release

    • Included in a patch release of the router core package.

@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 09b17b36-51c8-44a6-8744-f5b5f087be89

📥 Commits

Reviewing files that changed from the base of the PR and between d31406c and 2ad9de3.

📒 Files selected for processing (2)
  • packages/router-core/src/router.ts
  • packages/router-core/src/utils.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The change hoists regular expressions in router path, location, and URL utility code to module-level constants. It also changes the deepEqual signature and comparison handling. A patch changeset documents the update for @tanstack/router-core.

Changes

Router core optimization and comparison updates

Layer / File(s) Summary
Path normalization and parameter encoding
packages/router-core/src/path.ts, .changeset/hoist-hot-regex.md
cleanPath, trimPathRight, and encodeParam use module-level regex constants. The changeset declares a patch release.
Deep-equality comparison updates
packages/router-core/src/utils.ts, packages/router-core/src/router.ts
deepEqual now accepts positional flags. Undefined-key handling, reference-equality handling, and object-key comparison changed. matchRoute passes the partial flag as true.
Location and URL utility regexes
packages/router-core/src/router.ts, packages/router-core/src/utils.ts
parseLocation, decodePath, and encodePathLikeUrl use hoisted regex constants with the same matching patterns.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Refactor

Suggested reviewers: sheraff

Merge Risk: ⚪ Minimal · up to 2ad9d

The change is ready to merge; no concrete user-facing correctness or reliability risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: hoisting frequently evaluated regular expression literals in router-core.
Description check ✅ Passed The description includes the required Changes, Checklist, and Release Impact sections. It explains the motivation, measured impact, verification, and changeset. One checklist item remains unchecked, b…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/hoist-hot-regex

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nx-cloud

nx-cloud Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit 2ad9de3

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 12m 51s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 2m 4s View ↗

☁️ Nx Cloud last updated this comment at 2026-09-15 21:13:43 UTC

@pkg-pr-new

pkg-pr-new Bot commented Sep 14, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@8425

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@8425

@tanstack/eslint-plugin-start

npm i https://pkg.pr.new/@tanstack/eslint-plugin-start@8425

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@8425

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/@tanstack/nitro-v2-vite-plugin@8425

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@8425

@tanstack/react-router-devtools

npm i https://pkg.pr.new/@tanstack/react-router-devtools@8425

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/@tanstack/react-router-ssr-query@8425

@tanstack/react-start

npm i https://pkg.pr.new/@tanstack/react-start@8425

@tanstack/react-start-client

npm i https://pkg.pr.new/@tanstack/react-start-client@8425

@tanstack/react-start-rsc

npm i https://pkg.pr.new/@tanstack/react-start-rsc@8425

@tanstack/react-start-server

npm i https://pkg.pr.new/@tanstack/react-start-server@8425

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@8425

@tanstack/router-core

npm i https://pkg.pr.new/@tanstack/router-core@8425

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@8425

@tanstack/router-devtools-core

npm i https://pkg.pr.new/@tanstack/router-devtools-core@8425

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@8425

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@8425

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/@tanstack/router-ssr-query-core@8425

@tanstack/router-utils

npm i https://pkg.pr.new/@tanstack/router-utils@8425

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@8425

@tanstack/solid-router

npm i https://pkg.pr.new/@tanstack/solid-router@8425

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/@tanstack/solid-router-devtools@8425

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/@tanstack/solid-router-ssr-query@8425

@tanstack/solid-start

npm i https://pkg.pr.new/@tanstack/solid-start@8425

@tanstack/solid-start-client

npm i https://pkg.pr.new/@tanstack/solid-start-client@8425

@tanstack/solid-start-server

npm i https://pkg.pr.new/@tanstack/solid-start-server@8425

@tanstack/start-client-core

npm i https://pkg.pr.new/@tanstack/start-client-core@8425

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/@tanstack/start-fn-stubs@8425

@tanstack/start-plugin-core

npm i https://pkg.pr.new/@tanstack/start-plugin-core@8425

@tanstack/start-server-core

npm i https://pkg.pr.new/@tanstack/start-server-core@8425

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/@tanstack/start-static-server-functions@8425

@tanstack/start-storage-context

npm i https://pkg.pr.new/@tanstack/start-storage-context@8425

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@8425

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@8425

@tanstack/vue-router

npm i https://pkg.pr.new/@tanstack/vue-router@8425

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/@tanstack/vue-router-devtools@8425

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/@tanstack/vue-router-ssr-query@8425

@tanstack/vue-start

npm i https://pkg.pr.new/@tanstack/vue-start@8425

@tanstack/vue-start-client

npm i https://pkg.pr.new/@tanstack/vue-start-client@8425

@tanstack/vue-start-server

npm i https://pkg.pr.new/@tanstack/vue-start-server@8425

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@8425

commit: 2ad9de3

@github-actions

github-actions Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

4 package(s) bumped directly, 19 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/react-router 1.170.36 → 1.170.37 Changeset
@tanstack/router-core 1.171.30 → 1.171.31 Changeset
@tanstack/solid-router 1.170.34 → 1.170.35 Changeset
@tanstack/vue-router 1.170.33 → 1.170.34 Changeset
@tanstack/react-start 1.168.54 → 1.168.55 Dependent
@tanstack/react-start-client 1.168.34 → 1.168.35 Dependent
@tanstack/react-start-rsc 0.1.53 → 0.1.54 Dependent
@tanstack/react-start-server 1.167.41 → 1.167.42 Dependent
@tanstack/router-cli 1.167.36 → 1.167.37 Dependent
@tanstack/router-generator 1.167.36 → 1.167.37 Dependent
@tanstack/router-plugin 1.168.38 → 1.168.39 Dependent
@tanstack/router-vite-plugin 1.167.38 → 1.167.39 Dependent
@tanstack/solid-start 1.168.52 → 1.168.53 Dependent
@tanstack/solid-start-client 1.168.33 → 1.168.34 Dependent
@tanstack/solid-start-server 1.167.40 → 1.167.41 Dependent
@tanstack/start-client-core 1.170.30 → 1.170.31 Dependent
@tanstack/start-plugin-core 1.171.44 → 1.171.45 Dependent
@tanstack/start-server-core 1.169.35 → 1.169.36 Dependent
@tanstack/start-static-server-functions 1.167.35 → 1.167.36 Dependent
@tanstack/start-storage-context 1.167.32 → 1.167.33 Dependent
@tanstack/vue-start 1.168.51 → 1.168.52 Dependent
@tanstack/vue-start-client 1.167.36 → 1.167.37 Dependent
@tanstack/vue-start-server 1.167.40 → 1.167.41 Dependent

@github-actions

github-actions Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Benchmarks

  • Commit: 4fc006a89dae
  • Measured at: 2026-09-15T20:59:33.205Z
  • Baseline source: history:83895f127216
  • Dashboard: bundle-size history

The following scenarios have bundle-size changes compared with the baseline:

Scenario Current (gzip) Initial (gzip) Raw Brotli Trend
react-router.minimal 84.0 KiB
+44 B
83.9 KiB
+44 B
261.7 KiB
+56 B
73.3 KiB
+82 B
▂▂▂▇▇▇▇▁▁▁▁█
react-router.full 87.5 KiB
+55 B
87.4 KiB
+53 B
273.3 KiB
+54 B
76.3 KiB
+125 B
▄▄▃████▂▁▁▁█
solid-router.minimal 33.5 KiB
+44 B
33.3 KiB
+44 B
95.9 KiB
+59 B
30.3 KiB
+122 B
▆▆▅▃▃▃▃▁▁▁▁█
solid-router.full 38.3 KiB
+47 B
38.2 KiB
+46 B
110.6 KiB
+59 B
34.5 KiB
+35 B
▄▄▃▃▃▃▃▁▁▁▁█
vue-router.minimal 49.5 KiB
+46 B
49.4 KiB
+45 B
137.1 KiB
+59 B
44.9 KiB
+48 B
███▃▃▃▃▁▁▁▁▄
vue-router.full 55.1 KiB
+53 B
55.0 KiB
+51 B
155.3 KiB
+59 B
49.7 KiB
+31 B
███▃▃▃▃▁▁▁▁▄
react-start.minimal 96.9 KiB
+37 B
96.8 KiB
+37 B
303.9 KiB
+36 B
84.0 KiB
+14 B
▂▂▁████▄▄▄▄▇
react-start.query-integration 104.3 KiB
+37 B
104.2 KiB
+40 B
330.4 KiB
+57 B
90.5 KiB
+42 B
▂▂▁████▄▄▄▄█
react-start.deferred-hydration 97.7 KiB
+36 B
96.8 KiB
+36 B
305.2 KiB
+36 B
84.6 KiB
+20 B
▁▁▁████▃▃▃▃▆
react-start.full 100.1 KiB
+50 B
100.0 KiB
+48 B
313.6 KiB
+58 B
86.8 KiB
-1 B
▁▁▁▇▇▇▇▃▃▃▃█
react-start.rsbuild.minimal 100.2 KiB
+45 B
100.1 KiB
+45 B
314.3 KiB
+50 B
86.5 KiB
+53 B
▁▁▁▇▇▇▇▆▆▆▆█
react-start.rsbuild.minimal-iife 100.7 KiB
+43 B
100.5 KiB
+43 B
315.3 KiB
+50 B
86.9 KiB
-1 B
▁▁▁▇▇▇▇▆▆▆▆█
react-start.rsbuild.full 103.5 KiB
+35 B
103.3 KiB
+35 B
324.4 KiB
+50 B
89.3 KiB
+119 B
▁▁▁████▅▅▅▅▇
solid-start.minimal 46.4 KiB
+37 B
46.3 KiB
+40 B
137.1 KiB
+59 B
41.2 KiB
-31 B
▂▂▁▆▆▆▆▃▃▃▃█
solid-start.deferred-hydration 49.5 KiB
+47 B
46.3 KiB
+49 B
144.4 KiB
+59 B
44.0 KiB
+41 B
▁▁▂▅▅▅▅▂▂▂▂█
solid-start.full 51.5 KiB
+56 B
51.3 KiB
+58 B
152.4 KiB
+59 B
45.6 KiB
+10 B
▁▁▁▅▅▅▅▃▃▃▃█
vue-start.minimal 65.7 KiB
+46 B
65.5 KiB
+45 B
187.9 KiB
+59 B
58.6 KiB
+45 B
███▃▃▃▃▁▁▁▁▄
vue-start.full 69.5 KiB
+51 B
69.4 KiB
+49 B
200.3 KiB
+59 B
61.9 KiB
+150 B
███▄▄▄▄▁▁▁▁▅

Current gzip tracks all emitted client JS chunks. Initial gzip tracks only the entry/import graph. Trend sparkline is historical current gzip ending with this PR measurement; lower is better.

@codspeed

codspeed Bot commented Sep 14, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 5.03%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 179 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory mem client unique-location-churn (vue) 2 MB 2.1 MB -5.03%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing codex/hoist-hot-regex (2ad9de3) with main (83895f1)

Open in CodSpeed

A regex literal creates a new RegExp object every time it is evaluated
(measured: 56 B per evaluation, 0 once hoisted). decodePath and
encodePathLikeUrl run on every buildLocation, cleanPath on every href of a
basepath router, encodeParam on every splat and trimPathRight on every
trailing-slash path; parseLocation's fast-path test runs per navigation.
Their literals are now module constants. trimPathLeft (construction only)
and the cold branches of getUrlScheme are left as they were.

Allocation per buildLocation (sampling heap profiler over 160k builds against
the production dist, Node 24.8; on top of the previous commit):

  relative destination   1309 -> 1197 B  (-112)
  search updater         1085 ->  969 B  (-116)
  static destination      955 ->  786 B  (-169)

CPU (tests/build-location.bench.ts, two rounds, hz): within noise,
directionally +1-2%: search updater 41.6k/44.2k -> 43.5k/43.2k, relative
29.8k/30.2k -> 29.6k/30.3k, masked 20.5k/20.9k -> 20.8k/20.6k.

Bundle: react-router.minimal +18 gzip (derived from the composed measurements
86077 with, 86059 without these hoists).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
(cherry picked from commit 72b093cad28641112858bfbc9140175eb2c1a3da)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants