Skip to content

perf(router-core): memoize searchStr by search-object identity in buildLocation - #8143

Draft
Sheraff wants to merge 2 commits into
mainfrom
perf/task2-search-reserialization
Draft

perf(router-core): memoize searchStr by search-object identity in buildLocation#8143
Sheraff wants to merge 2 commits into
mainfrom
perf/task2-search-reserialization

Conversation

@Sheraff

@Sheraff Sheraff commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • buildLocation ran nullReplaceEqualDeep(fromSearch, nextSearch) (which returns the previous reference when unchanged) but then unconditionally called stringifySearch — rebuilding a URLSearchParams and probing every value even when nothing changed.
  • Adds an internal identity-keyed memo (WeakMap<object,string>, ~13 lines, router.ts only): structural sharing makes the returned reference a free memo key, so unchanged-search navigations reuse the cached string.

Performance

scenario before after delta
end-to-end real buildLocation, same-search navs 342 hz (2.92ms) 694 hz (1.44ms) 2.03x
flow micro, same-search small 1.94x
flow micro, same-search medium (~10 keys) 3.20x
flow micro, same-search large/nested 1.62x
changed-search navs ±1% (unchanged)

Root cause confirmed: stringifySearch costs ~2× nullReplaceEqualDeep and ran unconditionally.

Verification

  • Correctness: identity-keyed + immutable search objects ⇒ emitted searchStr always matches emitted search; hash-only hrefs verified (/?page=1#section) + round-trip assertions in new tests/searchStrMemo.test.ts.
  • test:unit ✅ (107 files / 1610 tests, +1 new) · eslint ✅ · types ✅
  • Full analysis: RESULT-perf-task2.md on the branch.

@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 5b1c51dc-f1fb-440e-b226-cc63ddb1783c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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 Aug 21, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit 0e2312b

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

☁️ Nx Cloud last updated this comment at 2026-08-22 06:06:08 UTC

@github-actions

github-actions Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Benchmarks

  • Commit: 101d10c9bd4a
  • Measured at: 2026-08-22T05:52:53.817Z
  • Baseline source: history:d83a8964c250
  • 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 83.9 KiB
+42 B
83.8 KiB
+44 B
262.9 KiB
+155 B
73.0 KiB
+7 B
▃▂▂▁▁▃▃▃▂▄▄█
react-router.full 87.4 KiB
+41 B
87.3 KiB
+43 B
274.6 KiB
+155 B
76.0 KiB
+10 B
▃▂▂▁▁▄▄▄▂▄▄█
solid-router.minimal 33.2 KiB
+43 B
33.1 KiB
+43 B
96.8 KiB
+155 B
30.0 KiB
+33 B
▁▁▁▄▄▅▅▅▅▆▆█
solid-router.full 38.1 KiB
+47 B
38.0 KiB
+46 B
111.4 KiB
+155 B
34.3 KiB
+30 B
▁▁▁▅▅▆▆▆▅▆▆█
vue-router.minimal 49.6 KiB
+48 B
49.5 KiB
+48 B
138.8 KiB
+155 B
44.8 KiB
+121 B
▁▁▁▁▁▄▄▄▂▄▄█
vue-router.full 55.2 KiB
+57 B
55.1 KiB
+55 B
157.0 KiB
+155 B
49.7 KiB
+7 B
▁▁▁▁▁▄▄▄▂▃▃█
react-start.minimal 96.8 KiB
+48 B
96.7 KiB
+47 B
305.2 KiB
+155 B
83.8 KiB
+38 B
▂▁▁▄▄▆▆▆▅▆▆█
react-start.deferred-hydration 97.5 KiB
+47 B
96.7 KiB
+46 B
306.6 KiB
+155 B
84.5 KiB
+53 B
▂▁▁▄▄▆▆▆▅▆▆█
react-start.full 100.0 KiB
+41 B
99.8 KiB
+41 B
314.9 KiB
+155 B
86.6 KiB
+48 B
▁▁▁▄▄▅▅▅▅▆▆█
react-start.rsbuild.minimal 100.1 KiB
+46 B
99.9 KiB
+46 B
315.5 KiB
+159 B
86.4 KiB
-49 B
▂▁▁▄▄▇▇▇▅▆▆█
react-start.rsbuild.minimal-iife 100.5 KiB
+46 B
100.3 KiB
+46 B
316.4 KiB
+159 B
86.7 KiB
+17 B
▂▁▁▄▄▇▇▇▅▆▆█
react-start.rsbuild.full 103.4 KiB
+46 B
103.3 KiB
+46 B
325.6 KiB
+159 B
89.0 KiB
+58 B
▂▁▁▄▄▇▇▇▅▆▆█
solid-start.minimal 46.1 KiB
+40 B
46.0 KiB
+42 B
137.9 KiB
+155 B
41.0 KiB
+23 B
▁▁▁▆▆▇▇▇▆▇▇█
solid-start.deferred-hydration 49.2 KiB
+46 B
46.0 KiB
+49 B
145.4 KiB
+155 B
43.8 KiB
+49 B
▁▁▁▆▆▆▆▆▅▆▆█
solid-start.full 51.2 KiB
+49 B
51.0 KiB
+48 B
153.3 KiB
+155 B
45.4 KiB
+65 B
▁▁▁▅▅▆▆▆▆▆▆█
vue-start.minimal 65.7 KiB
+42 B
65.6 KiB
+45 B
189.7 KiB
+155 B
58.5 KiB
+32 B
▁▁▁▅▅▆▆▆▅▆▆█
vue-start.full 69.6 KiB
+47 B
69.4 KiB
+43 B
202.0 KiB
+155 B
61.8 KiB
+43 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.

@github-actions

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.31 → 1.170.32 Changeset
@tanstack/router-core 1.171.26 → 1.171.27 Changeset
@tanstack/solid-router 1.170.29 → 1.170.30 Changeset
@tanstack/vue-router 1.170.28 → 1.170.29 Changeset
@tanstack/react-start 1.168.48 → 1.168.49 Dependent
@tanstack/react-start-client 1.168.29 → 1.168.30 Dependent
@tanstack/react-start-rsc 0.1.47 → 0.1.48 Dependent
@tanstack/react-start-server 1.167.36 → 1.167.37 Dependent
@tanstack/router-cli 1.167.32 → 1.167.33 Dependent
@tanstack/router-generator 1.167.32 → 1.167.33 Dependent
@tanstack/router-plugin 1.168.34 → 1.168.35 Dependent
@tanstack/router-vite-plugin 1.167.34 → 1.167.35 Dependent
@tanstack/solid-start 1.168.46 → 1.168.47 Dependent
@tanstack/solid-start-client 1.168.28 → 1.168.29 Dependent
@tanstack/solid-start-server 1.167.35 → 1.167.36 Dependent
@tanstack/start-client-core 1.170.26 → 1.170.27 Dependent
@tanstack/start-plugin-core 1.171.38 → 1.171.39 Dependent
@tanstack/start-server-core 1.169.30 → 1.169.31 Dependent
@tanstack/start-static-server-functions 1.167.31 → 1.167.32 Dependent
@tanstack/start-storage-context 1.167.28 → 1.167.29 Dependent
@tanstack/vue-start 1.168.45 → 1.168.46 Dependent
@tanstack/vue-start-client 1.167.31 → 1.167.32 Dependent
@tanstack/vue-start-server 1.167.35 → 1.167.36 Dependent

@pkg-pr-new

pkg-pr-new Bot commented Aug 21, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

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

@tanstack/eslint-plugin-router

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

@tanstack/eslint-plugin-start

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

@tanstack/history

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

@tanstack/nitro-v2-vite-plugin

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

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

@tanstack/react-start-client

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

@tanstack/react-start-rsc

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

@tanstack/react-start-server

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

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

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

@tanstack/solid-start-client

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

@tanstack/solid-start-server

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

@tanstack/start-client-core

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

@tanstack/start-fn-stubs

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

@tanstack/start-plugin-core

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

@tanstack/start-server-core

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

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

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

@tanstack/valibot-adapter

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

@tanstack/virtual-file-routes

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

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

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

@tanstack/vue-start-client

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

@tanstack/vue-start-server

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

@tanstack/zod-adapter

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

commit: 0e2312b

nx-cloud[bot]

This comment was marked as outdated.

@codspeed-hq

codspeed-hq Bot commented Aug 21, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 8.34%

⚠️ 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

⚡ 4 improved benchmarks
❌ 6 regressed benchmarks
✅ 170 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory mem server error-paths redirect (solid) 382.2 KB 855.9 KB -55.34%
Memory mem client unique-location-churn (solid) 265.7 KB 306 KB -13.17%
Memory mem server error-paths unmatched (vue) 577.6 KB 641.3 KB -9.92%
Memory mem client unique-location-churn (vue) 467.7 KB 513 KB -8.83%
Memory mem server peak-large-page (solid) 1 MB 1.1 MB -5.64%
Memory mem server server-fn-churn (react) 378.9 KB 393.5 KB -3.73%
Memory mem server error-paths unmatched (react) 544.1 KB 435.3 KB +24.99%
Memory mem server error-paths redirect (react) 318.9 KB 296.4 KB +7.59%
Memory mem server aborted-requests (vue) 1.1 MB 1 MB +4.19%
Memory mem server peak-large-page (react) 1.2 MB 1.2 MB +3.32%

Tip

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


Comparing perf/task2-search-reserialization (0e2312b) with main (d83a896)

Open in CodSpeed

@nx-cloud nx-cloud Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nx Cloud is proposing a fix for your failed CI:

We updated the hash updater callbacks in the two new test files added by this PR to use optional parameters (prev?: string) instead of required ones, which fixes the TS2322 type errors caught by TypeScript TS56. The Updater<string> type expects (prev?: string | undefined) => string, and the ?? '' fallback preserves existing test assertions (e.g. '/?page=1#x-0' when no prior hash exists).

Tip

We verified this fix by re-running @tanstack/router-core:test:types, @tanstack/router-core:test:unit.

Suggested Fix changes
diff --git a/packages/router-core/tests/buildLocationSearchStr.bench.ts b/packages/router-core/tests/buildLocationSearchStr.bench.ts
index 43c92539..7e8c48a5 100644
--- a/packages/router-core/tests/buildLocationSearchStr.bench.ts
+++ b/packages/router-core/tests/buildLocationSearchStr.bench.ts
@@ -32,7 +32,7 @@ expect(loc.searchStr).toBe('?page=1')
 const hashLoc = warm.buildLocation({
   to: '/',
   search: true,
-  hash: (prev: string) => prev,
+  hash: (prev?: string) => prev ?? '',
 })
 expect(hashLoc.href.startsWith('/?page=1')).toBe(true)
 
@@ -43,8 +43,7 @@ describe('router.buildLocation - repeated same-search navigations', () => {
       const router = makeRouter()
       let size = 0
       for (let i = 0; i < iterations; i++) {
-        size += router.buildLocation({ to: '/', search: true }).searchStr
-          .length
+        size += router.buildLocation({ to: '/', search: true }).searchStr.length
       }
       benchmarkSink = size
     },
diff --git a/packages/router-core/tests/searchStrMemo.test.ts b/packages/router-core/tests/searchStrMemo.test.ts
index 3c2c5dfe..4e5c651f 100644
--- a/packages/router-core/tests/searchStrMemo.test.ts
+++ b/packages/router-core/tests/searchStrMemo.test.ts
@@ -41,7 +41,7 @@ test('hash-only nav: href contains unchanged search plus hash', () => {
   const h2 = router.buildLocation({
     to: '/',
     search: true,
-    hash: (prev: string) => `x-${prev.length}`,
+    hash: (prev?: string) => `x-${(prev ?? '').length}`,
   })
   expect(h2.href).toBe('/?page=1#x-0')
 })

Apply fix via Nx Cloud  Reject fix via Nx Cloud


Or Apply changes locally with:

npx nx-cloud apply-locally jI4t-echy

Apply fix locally with your editor ↗   View interactive diff ↗



🎓 Learn more about Self-Healing CI on nx.dev

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.

1 participant