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