Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-router): improve performance of Link #2359

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

honzahruby
Copy link
Contributor

Addresses #2011 to improve performance of Link and router.buildLocation.

matchRoutesInternal traverses all routes in flatRoutes, which degrades the performance of Link with the number of routes registered. This PR adds an optimization to look up the route in routesByPath in constant time for absolute paths in to.

Rendering times of 10000 Links as described in the original discussion thread #2011 (comment)

2 routes 400+ routes 400+ routes optimized
hardcoded href 150ms 160ms 150ms
interpolatePath 180ms 190ms 180ms
buildLocation 360ms 1760ms ⚠️ 320ms
Link component 660ms 2160ms ⚠️ 590ms

Copy link

nx-cloud bot commented Sep 18, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit f6015bf. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

): Array<AnyRouteMatch>
public matchRoutes(
next: ParsedLocation,
opts?: MatchRoutesOpts,
routeFullPath?: string,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

❓ Would it make sense to extend ParsedLocation with the full path instead?

Copy link

pkg-pr-new bot commented Sep 18, 2024

Open in Stackblitz

More templates

@tanstack/create-router

pnpm add https://pkg.pr.new/@tanstack/create-router@2359

@tanstack/history

pnpm add https://pkg.pr.new/@tanstack/history@2359

@tanstack/react-cross-context

pnpm add https://pkg.pr.new/@tanstack/react-cross-context@2359

@tanstack/react-router

pnpm add https://pkg.pr.new/@tanstack/react-router@2359

@tanstack/react-router-with-query

pnpm add https://pkg.pr.new/@tanstack/react-router-with-query@2359

@tanstack/router-arktype-adapter

pnpm add https://pkg.pr.new/@tanstack/router-arktype-adapter@2359

@tanstack/router-cli

pnpm add https://pkg.pr.new/@tanstack/router-cli@2359

@tanstack/router-generator

pnpm add https://pkg.pr.new/@tanstack/router-generator@2359

@tanstack/router-devtools

pnpm add https://pkg.pr.new/@tanstack/router-devtools@2359

@tanstack/router-plugin

pnpm add https://pkg.pr.new/@tanstack/router-plugin@2359

@tanstack/router-valibot-adapter

pnpm add https://pkg.pr.new/@tanstack/router-valibot-adapter@2359

@tanstack/router-vite-plugin

pnpm add https://pkg.pr.new/@tanstack/router-vite-plugin@2359

@tanstack/start

pnpm add https://pkg.pr.new/@tanstack/start@2359

@tanstack/router-zod-adapter

pnpm add https://pkg.pr.new/@tanstack/router-zod-adapter@2359

@tanstack/start-vite-plugin

pnpm add https://pkg.pr.new/@tanstack/start-vite-plugin@2359

@tanstack/virtual-file-routes

pnpm add https://pkg.pr.new/@tanstack/virtual-file-routes@2359

commit: f6015bf

@honzahruby honzahruby changed the title feat(react-router): fast route match for full path feat(react-router): improve performance of Link Sep 18, 2024
@schiller-manuel
Copy link
Contributor

how did you measure the time?
would make sense to add this benchmark to the repo

const foundRoute = this.flatRoutes.find((route) => {
let foundRoute: AnyRoute | undefined = routeFullPath
? this.routesByPath[routeFullPath]
: undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

does this mean if to is not specified, the link will still be slow?

in this case, can we use the current location of router instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it a valid use case to use a Link without to? If yes, we could provide the current match's fullPath as a fallback.

Currently, it only speeds up the resolve process for links with absolute paths, because they can be easily looked up in the routeById map. Is there any util to quickly resolve relative paths?

@honzahruby
Copy link
Contributor Author

how did you measure the time? would make sense to add this benchmark to the repo

I used this code in the large-file-based example and measured the time to render the Home component in react dev tools. It renders 10000 links with generated IDs used as a route param.

Is there any performance benchmark setup in this repo? How about https://www.npmjs.com/package/reassure?

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