Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

link.bench.tsx

186 lines4.5 KB

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