Open History to see the last commit
1import { render } from '@testing-library/react'
2import { bench, describe } from 'vitest'
3import { getRouteSegments } from '@tanstack/router-core'
4import {
5 Link,
6 RouterProvider,
7 createMemoryHistory,
8 createRootRoute,
9 createRoute,
10 createRouter,
11 interpolatePath,
12 useRouter,
13} from '../src'
14import type { LinkProps } from '../src'
15β
16const createRouterRenderer =
17 (routesCount: number) => (children: React.ReactNode) => {
18 const rootRoute = createRootRoute()
19 const indexRoute = createRoute({
20 getParentRoute: () => rootRoute,
21 path: '/',
22 component: () => children,
23 })
24 const paramRoutes = Array.from({ length: routesCount }).map((_, i) =>
25 createRoute({
26 getParentRoute: () => rootRoute,
27 path: `/params/$param${i}`,
28 }),
29 )
30 const routeTree = rootRoute.addChildren([indexRoute, ...paramRoutes])
31 return createRouter({
32 routeTree,
33 history: createMemoryHistory({ initialEntries: ['/'] }),
34 })
35 }
36β
37const InterpolatePathLink = ({
38 to,
39 params,
40 children,
41}: React.PropsWithChildren<{
42 to: string
43 params: Record<string, number>
44}>) => {
45 const router = useRouter()
46 const href = interpolatePath(
47 to,
48 getRouteSegments(router.routesByPath[to])!,
49 params,
50 )
51 return <a href={href}>{children}</a>
52}
53β
54const BuildLocationLink = ({
55 children,
56 ...props
57}: React.PropsWithChildren<LinkProps>) => {
58 const router = useRouter()
59 const { href } = router.buildLocation(props)
60 return <a href={href}>{children}</a>
61}
62β
63describe.each([
64 {
65 name: 'small router',
66 numberOfRoutes: 1,
67 matchedParamId: 0, // range from 0 to numberOfRoutes-1
68 numberOfLinks: 5000,
69 },
70 {
71 name: 'medium router',
72 numberOfRoutes: 1000,
73 matchedParamId: 500, // range from 0 to numberOfRoutes-1
74 numberOfLinks: 5000,
75 },
76 // {
77 // name: 'large router',
78 // numberOfRoutes: 10000,
79 // matchedParamId: 9999, // range from 0 to numberOfRoutes-1
80 // numberOfLinks: 15000,
81 // },
82])('$name', ({ numberOfRoutes, numberOfLinks, matchedParamId }) => {
83 const renderRouter = createRouterRenderer(numberOfRoutes)
84β
85 bench(
86 'hardcoded href',
87 () => {
88 const router = renderRouter(
89 Array.from({ length: numberOfLinks }).map((_, i) => (
90 <a key={i} href={`/params/${i}`}>
91 {i}
92 </a>
93 )),
94 )
95 render(<RouterProvider router={router} />)
96 },
97 { warmupIterations: 1 },
98 )
99β
100 bench(
101 'interpolate path',
102 () => {
103 const router = renderRouter(
104 Array.from({ length: numberOfLinks }).map((_, i) => (
105 <InterpolatePathLink
106 key={i}
107 to={`/params/$param${Math.min(i, matchedParamId)}`}
108 params={{ [`param${Math.min(i, matchedParamId)}`]: i }}
109 >
110 {i}
111 </InterpolatePathLink>
112 )),
113 )
114 render(<RouterProvider router={router} />)
115 },
116 { warmupIterations: 1 },
117 )
118β
119 bench(
120 'build location',
121 () => {
122 const router = renderRouter(
123 Array.from({ length: numberOfLinks }).map((_, i) => (
124 <BuildLocationLink
125 key={i}
126 to={`/params/$param${Math.min(i, matchedParamId)}`}
127 params={{ [`param${Math.min(i, matchedParamId)}`]: i }}
128 >
129 {i}
130 </BuildLocationLink>
131 )),
132 )
133 render(<RouterProvider router={router} />)
134 },
135 { warmupIterations: 1 },
136 )
137β
138 bench(
139 'link to absolute path',
140 () => {
141 const router = renderRouter(
142 Array.from({ length: numberOfLinks }).map((_, i) => (
143 <Link
144 key={i}
145 to={`/params/$param${Math.min(i, matchedParamId)}`}
146 params={{ [`param${Math.min(i, matchedParamId)}`]: i }}
147 >
148 {i}
149 </Link>
150 )),
151 )
152 render(<RouterProvider router={router} />)
153 },
154 { warmupIterations: 1 },
155 )
156β
157 bench(
158 'link to relative path',
159 () => {
160 const router = renderRouter(
161 Array.from({ length: numberOfLinks }).map((_, i) => {
162 const to = `./params/$param${Math.min(i, matchedParamId)}`
163β
164 return (
165 <Link
166 key={i}
167 from="/"
168 to={to}
169 params={{ [`param${Math.min(i, matchedParamId)}`]: i }}
170 >
171 {i}
172 </Link>
173 )
174 }),
175 )
176 render(<RouterProvider router={router} />)
177 },
178 { warmupIterations: 1 },
179 )
180β
181 bench(
182 'link to current path',
183 () => {
184 const router = renderRouter(
185 Array.from({ length: numberOfLinks }).map((_, i) => (
186 <Link key={i} from="/" search={{ param: i }}>
187 {i}
188 </Link>
189 )),
190 )
191 render(<RouterProvider router={router} />)
192 },
193 { warmupIterations: 1 },
194 )
195})