Skip to content

refactor(router-core): matchRoutesInternal minor performance improvements - #6560

Merged
Sheraff merged 3 commits into
mainfrom
refactor-router-core-match-routes-internal-minor-perf
Jan 31, 2026
Merged

refactor(router-core): matchRoutesInternal minor performance improvements#6560
Sheraff merged 3 commits into
mainfrom
refactor-router-core-match-routes-internal-minor-perf

Conversation

@Sheraff

@SheraffSheraff commented Jan 31, 2026

Copy link
Copy Markdown
Collaborator

Some minor performance optimizations on matchRoutesInternal

  • getParentContext is separate, because it doesn't need the scope of matchRoutesInternal
  • the matches array has a size known in advance, so we can avoid push
  • use for loops instead of .forEach()
  • avoid IIFE and instead use let + a code block

Before

[1] 91k requests in 30.01s, 808 MB read
[1] [1] === SSR Benchmark Results ===
[1] Total requests: 71119
[1] Requests/sec: 2371.1
[1] Latency (avg): 2.78ms
[1] Latency (p99): 8ms
[1] Throughput: 25.69 MB/s
Screenshot 2026-01-31 at 13 30 52

After

[1] 92k requests in 30.01s, 817 MB read
[1] [1] === SSR Benchmark Results ===
[1] Total requests: 71923
[1] Requests/sec: 2398
[1] Latency (avg): 2.75ms
[1] Latency (p99): 8ms
[1] Throughput: 25.98 MB/s
Screenshot 2026-01-31 at 13 30 48

Summary by CodeRabbit

  • Refactor
    • Internal router logic reorganized for more consistent route/context propagation and deterministic match ordering.
    • Improved handling of search parameters and validation errors to yield safer defaults and optional error throwing.
    • Match construction and finalization flow streamlined, preserving existing behavior while improving stability and maintainability.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitaiBot commented Jan 31, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The router core extracts parent context computation into a new private method getParentContext, replaces per-route callbacks with indexed for loops, refactors search-parameter resolution into deterministic local variables, and switches match updates from object spread to Object.assign, preserving existing behavior.

Changes

Cohort / File(s)Summary
Router context and match resolution
packages/router-core/src/router.ts
Added private getParentContext(parentMatch?). Refactored matchRoutesInternal to use indexed for loops, pre-sized matches array, deterministic local bindings for preMatchSearch/strictMatchSearch/searchError, conditional error throwing based on opts.throwOnError, and explicit Object.assign mutations when updating/creating matches. Context propagation now uses this.getParentContext(...) throughout.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • schiller-manuel

Poem

🐰 In loops I hop and tidy the trail,
Contexts gathered, no more detail frail,
Params sorted neat, errors caught with care,
Matches aligned, all set to share,
A rabbit's small cheer for code made fair 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check nameStatusExplanation
Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
Title check✅ PassedThe title clearly describes the main change: performance optimizations to matchRoutesInternal in router-core through refactoring, which aligns with the detailed changes in the PR.
Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor-router-core-match-routes-internal-minor-perf

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

@nx-cloud

nx-cloudBot commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit 6a46a8c

CommandStatusDurationResult
nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded14m 15sView ↗
nx run-many --target=build --exclude=examples/*...✅ Succeeded1m 44sView ↗

☁️ Nx Cloud last updated this comment at 2026-01-31 12:51:07 UTC

@pkg-pr-new

pkg-pr-newBot commented Jan 31, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6560

@tanstack/eslint-plugin-router

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

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6560

@tanstack/nitro-v2-vite-plugin

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6560

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6560

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6560

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6560

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6560

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6560

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6560

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-fn-stubs@6560

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6560

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6560

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6560

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6560

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6560

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6560

@tanstack/vue-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6560

@tanstack/vue-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6560

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6560

commit: 6a46a8c

@Sheraff
Sheraff merged commit 1ce1919 into mainJan 31, 2026
6 checks passed
@Sheraff
Sheraff deleted the refactor-router-core-match-routes-internal-minor-perf branch January 31, 2026 15:39
Sign up for freeto 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.

1 participant

@Sheraff