Skip to content

perf(router-core): skip impossible JSON parse attempts - #8006

Merged
Sheraff merged 3 commits into
mainfrom
agent/search-params-skip-json-parse
Aug 8, 2026
Merged

perf(router-core): skip impossible JSON parse attempts#8006
Sheraff merged 3 commits into
mainfrom
agent/search-params-skip-json-parse

Conversation

@Sheraff

@Sheraff Sheraff commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

What changed

  • avoid calling JSON.parse when a string's first character cannot begin valid JSON
  • apply the shortcut only when the configured parser is exactly JSON.parse
  • preserve custom parser behavior and all valid JSON prefixes, including whitespace, quotes, numbers, literals, arrays, and objects
  • add correctness cases and a focused benchmark covering expected and unfavorable distributions

Why

The default search serializer probes every string with JSON.parse so JSON-looking strings remain strings after a round trip. Ordinary application strings usually fail that probe, making exception creation and handling the dominant cost. The grammar check avoids only parses that are guaranteed to fail.

Impact

Focused local benchmarks, each serializing 1,000 objects:

  • ordinary strings: 15.694 ms to 4.571 ms (about 70.9% faster)
  • mixed application values: 4.669 ms to 0.482 ms (about 89.7% faster)
  • JSON-compatible strings remain on the parse path (0.863 ms in the latest control run)
  • strings beginning with JSON literal prefixes remain an intentionally unfavorable control (17.122 ms)

react-router.minimal changes from 85,919 to 85,956 bytes gzip (+37 bytes).

Validation

  • @tanstack/router-core:test:unit: 105 files / 1,547 tests passed, plus 3 expected failures
  • @tanstack/router-core:test:types: TypeScript 5.6 through 7.0 passed
  • @tanstack/router-core:test:eslint: zero errors (existing warnings remain)
  • focused tests/searchParams.bench.ts run
  • react-router.minimal bundle-size scenario on main and this branch
  • git diff --check

Summary by CodeRabbit

  • Bug Fixes

    • Improved search-parameter serialization by skipping unnecessary JSON parsing for strings that cannot contain valid JSON.
    • Preserved correct handling of JSON-compatible values, empty strings, punctuation, and custom parsers.
  • Performance

    • Reduced avoidable parsing work during query-string serialization.
  • Tests

    • Expanded coverage for serialization edge cases.
    • Added benchmarks for representative search-parameter scenarios.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 3ce06239-1eef-4f84-9e31-3c0ad504f4c5

📥 Commits

Reviewing files that changed from the base of the PR and between c568caf and e86a0d5.

📒 Files selected for processing (4)
  • .changeset/dirty-bushes-sink.md
  • packages/router-core/src/searchParams.ts
  • packages/router-core/tests/searchParams.bench.ts
  • packages/router-core/tests/searchParams.test.ts

📝 Walkthrough

Walkthrough

The search parameter serializer now avoids impossible JSON.parse calls by checking string prefixes. Tests and benchmarks cover default serialization, custom parsers, JSON-compatible strings, and edge cases. A patch changeset documents the router-core update.

Changes

Search serialization

Layer / File(s) Summary
Guard JSON parsing
packages/router-core/src/searchParams.ts
stringifyValue checks jsonStart and the exact JSON.parse function before parsing string values.
Validate serialization behavior
packages/router-core/tests/searchParams.test.ts, packages/router-core/tests/searchParams.bench.ts, .changeset/dirty-bushes-sink.md
Tests and benchmarks cover ordinary values, JSON-compatible strings, custom parsers, punctuation, empty strings, and mixed values. The changeset marks a patch release.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant stringifySearchWith
  participant stringifyValue
  participant JSON.parse
  stringifySearchWith->>stringifyValue: serialize search value
  stringifyValue->>stringifyValue: check jsonStart
  stringifyValue->>JSON.parse: parse eligible string
  JSON.parse-->>stringifyValue: parsed value
  stringifyValue-->>stringifySearchWith: serialized value
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main optimization: skipping impossible JSON parse attempts in router-core.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/search-params-skip-json-parse

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 8, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit e86a0d5

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

☁️ Nx Cloud last updated this comment at 2026-08-08 17:21:40 UTC

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

1 package(s) bumped directly, 22 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/router-core 1.171.19 → 1.171.20 Changeset
@tanstack/react-router 1.170.23 → 1.170.24 Dependent
@tanstack/react-start 1.168.40 → 1.168.41 Dependent
@tanstack/react-start-client 1.168.21 → 1.168.22 Dependent
@tanstack/react-start-rsc 0.1.39 → 0.1.40 Dependent
@tanstack/react-start-server 1.167.28 → 1.167.29 Dependent
@tanstack/router-cli 1.167.25 → 1.167.26 Dependent
@tanstack/router-generator 1.167.25 → 1.167.26 Dependent
@tanstack/router-plugin 1.168.27 → 1.168.28 Dependent
@tanstack/router-vite-plugin 1.167.27 → 1.167.28 Dependent
@tanstack/solid-router 1.170.22 → 1.170.23 Dependent
@tanstack/solid-start 1.168.39 → 1.168.40 Dependent
@tanstack/solid-start-client 1.168.21 → 1.168.22 Dependent
@tanstack/solid-start-server 1.167.28 → 1.167.29 Dependent
@tanstack/start-client-core 1.170.19 → 1.170.20 Dependent
@tanstack/start-plugin-core 1.171.31 → 1.171.32 Dependent
@tanstack/start-server-core 1.169.23 → 1.169.24 Dependent
@tanstack/start-static-server-functions 1.167.24 → 1.167.25 Dependent
@tanstack/start-storage-context 1.167.21 → 1.167.22 Dependent
@tanstack/vue-router 1.170.21 → 1.170.22 Dependent
@tanstack/vue-start 1.168.38 → 1.168.39 Dependent
@tanstack/vue-start-client 1.167.24 → 1.167.25 Dependent
@tanstack/vue-start-server 1.167.28 → 1.167.29 Dependent

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Benchmarks

  • Commit: efb9dfc55201
  • Measured at: 2026-08-08T17:17:34.467Z
  • Baseline source: history:c568caf3f376
  • Dashboard: bundle-size history

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

Scenario Current (gzip) Delta vs baseline Initial gzip Raw Brotli Trend
react-router.minimal 83.92 KiB +17 B (+0.02%) 83.78 KiB 262.67 KiB 72.99 KiB █████████▁▁▁
react-router.full 87.43 KiB +34 B (+0.04%) 87.29 KiB 274.39 KiB 76.15 KiB █████████▁▁▁
solid-router.minimal 33.24 KiB +25 B (+0.07%) 33.12 KiB 96.51 KiB 29.94 KiB █████████▁▁▁
solid-router.full 38.07 KiB +27 B (+0.07%) 37.95 KiB 111.14 KiB 34.25 KiB █████████▁▁▁
vue-router.minimal 49.53 KiB +36 B (+0.07%) 49.41 KiB 138.25 KiB 44.68 KiB █████████▁▁▁
vue-router.full 55.11 KiB +39 B (+0.07%) 54.99 KiB 156.43 KiB 49.59 KiB █████████▁▁▁
react-start.minimal 96.70 KiB +41 B (+0.04%) 96.57 KiB 304.83 KiB 83.82 KiB █████████▁▁▁
react-start.deferred-hydration 97.44 KiB +51 B (+0.05%) 96.59 KiB 306.19 KiB 84.42 KiB █████████▁▁▁
react-start.full 99.88 KiB +50 B (+0.05%) 99.75 KiB 314.55 KiB 86.52 KiB █████████▁▁▁
react-start.rsbuild.minimal 100.02 KiB +25 B (+0.02%) 99.85 KiB 315.37 KiB 86.20 KiB ▁▁▁▆▆▅▅▆▆▆▆█
react-start.rsbuild.minimal-iife 100.42 KiB +22 B (+0.02%) 100.26 KiB 316.31 KiB 86.58 KiB ▁▁▁▆▆▅▅▆▆▆▆█
react-start.rsbuild.full 103.35 KiB +34 B (+0.03%) 103.17 KiB 325.46 KiB 88.99 KiB ▁▁▁▅▅▄▄▆▆▆▆█
solid-start.minimal 45.97 KiB +31 B (+0.07%) 45.85 KiB 137.51 KiB 40.93 KiB █████████▁▁▁
solid-start.deferred-hydration 49.04 KiB +37 B (+0.07%) 45.90 KiB 144.97 KiB 43.65 KiB █████████▁▁▁
solid-start.full 51.05 KiB +37 B (+0.07%) 50.93 KiB 152.90 KiB 45.29 KiB █████████▁▁▁
vue-start.minimal 65.52 KiB +31 B (+0.05%) 65.39 KiB 189.02 KiB 58.38 KiB █████████▁▁▁
vue-start.full 69.34 KiB +55 B (+0.08%) 69.21 KiB 201.30 KiB 61.58 KiB █████████▁▁▁

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.

@pkg-pr-new

pkg-pr-new Bot commented Aug 8, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

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

@tanstack/eslint-plugin-router

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

@tanstack/eslint-plugin-start

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

@tanstack/history

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

@tanstack/nitro-v2-vite-plugin

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

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

@tanstack/react-start-client

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

@tanstack/react-start-rsc

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

@tanstack/react-start-server

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

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

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

@tanstack/solid-start-client

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

@tanstack/solid-start-server

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

@tanstack/start-client-core

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

@tanstack/start-fn-stubs

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

@tanstack/start-plugin-core

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

@tanstack/start-server-core

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

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

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

@tanstack/valibot-adapter

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

@tanstack/virtual-file-routes

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

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

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

@tanstack/vue-start-client

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

@tanstack/vue-start-server

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

@tanstack/zod-adapter

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

commit: e86a0d5

@codspeed-hq

codspeed-hq Bot commented Aug 8, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 0.36%

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

⚡ 23 improved benchmarks
❌ 7 (👁 7) regressed benchmarks
✅ 150 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory mem server error-paths redirect (solid) 588.2 KB 288.5 KB ×2
Simulation ssr request loop (react) 80 ms 51.8 ms +54.26%
Simulation client-search-params navigation loop (react) 59.4 ms 47.1 ms +26.14%
Simulation ssr rewrite localized (react) 89 ms 75.3 ms +18.2%
Simulation client-search-params navigation loop (vue) 80.5 ms 68.4 ms +17.63%
Simulation client-links navigation loop (react) 90.6 ms 77.2 ms +17.41%
Simulation client-search-params navigation loop (solid) 114.8 ms 99.9 ms +14.93%
Simulation ssr request loop (solid) 158 ms 144.4 ms +9.41%
Simulation ssr rewrite passthrough (react) 114.1 ms 104.6 ms +9.08%
Simulation client-links navigation loop (vue) 163.9 ms 150.5 ms +8.92%
Simulation ssr rewrite localized (solid) 126.6 ms 117 ms +8.17%
Simulation client-side navigation loop (react) 41.2 ms 38.1 ms +8.05%
Simulation ssr request loop (vue) 270.2 ms 254.9 ms +6%
Memory mem client preload-churn (vue) 776.2 KB 735.3 KB +5.57%
Simulation client-rewrites navigation loop (react) 44.6 ms 42.4 ms +5.21%
Simulation client-rewrites navigation loop (vue) 48.1 ms 45.9 ms +4.77%
Simulation client-side navigation loop (vue) 70.1 ms 67 ms +4.6%
Simulation client-side navigation loop (solid) 88 ms 84.4 ms +4.26%
Simulation ssr rewrite localized (vue) 214.6 ms 206.9 ms +3.74%
Memory mem server serialization-payload (solid) 3.5 MB 3.4 MB +3.51%
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing agent/search-params-skip-json-parse (e86a0d5) with main (c568caf)

Open in CodSpeed

@Sheraff
Sheraff marked this pull request as ready for review August 8, 2026 17:14
@Sheraff
Sheraff merged commit 44a8c3e into main Aug 8, 2026
26 checks passed
@Sheraff
Sheraff deleted the agent/search-params-skip-json-parse branch August 8, 2026 17:43
@github-actions github-actions Bot mentioned this pull request Aug 8, 2026
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