Open History to see the last commit
1import { render } from '@solidjs/testing-library'
2import { describe, test } 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 test('hardcoded href', async ({ bench }) => {
87 await bench('hardcoded href', () => {
88 const router = renderRouter(
89 Array.from({ length: numberOfLinks }).map((_, i) => (
90 <a href={`/params/${i}`}>{i}</a>
91 )),
92 )
93 render(() => <RouterProvider router={router} />)
94 }).run({
95 warmupIterations: 1,
96 })
97 })
98β
99 test('interpolate path', async ({ bench }) => {
100 await bench('interpolate path', () => {
101 const router = renderRouter(
102 Array.from({ length: numberOfLinks }).map((_, i) => (
103 <InterpolatePathLink
104 to={`/params/$param${Math.min(i, matchedParamId)}`}
105 params={{ [`param${Math.min(i, matchedParamId)}`]: i }}
106 >
107 {i}
108 </InterpolatePathLink>
109 )),
110 )
111 render(() => <RouterProvider router={router} />)
112 }).run({
113 warmupIterations: 1,
114 })
115 })
116β
117 test('build location', async ({ bench }) => {
118 await bench('build location', () => {
119 const router = renderRouter(
120 Array.from({ length: numberOfLinks }).map((_, i) => (
121 <BuildLocationLink
122 to={`/params/$param${Math.min(i, matchedParamId)}`}
123 params={{ [`param${Math.min(i, matchedParamId)}`]: i }}
124 >
125 {i}
126 </BuildLocationLink>
127 )),
128 )
129 render(() => <RouterProvider router={router} />)
130 }).run({
131 warmupIterations: 1,
132 })
133 })
134β
135 test('link to absolute path', async ({ bench }) => {
136 await bench('link to absolute path', () => {
137 const router = renderRouter(
138 Array.from({ length: numberOfLinks }).map((_, i) => (
139 <Link
140 to={`/params/$param${Math.min(i, matchedParamId)}`}
141 params={{ [`param${Math.min(i, matchedParamId)}`]: i }}
142 >
143 {i}
144 </Link>
145 )),
146 )
147 render(() => <RouterProvider router={router} />)
148 }).run({
149 warmupIterations: 1,
150 })
151 })
152β
153 test('link to relative path', async ({ bench }) => {
154 await bench('link to relative path', () => {
155 const router = renderRouter(
156 Array.from({ length: numberOfLinks }).map((_, i) => {
157 const to = `./params/$param${Math.min(i, matchedParamId)}`
158β
159 return (
160 <Link
161 from="/"
162 to={to}
163 params={{ [`param${Math.min(i, matchedParamId)}`]: i }}
164 >
165 {i}
166 </Link>
167 )
168 }),
169 )
170 render(() => <RouterProvider router={router} />)
171 }).run({
172 warmupIterations: 1,
173 })
174 })
175β
176 test('link to current path', async ({ bench }) => {
177 await bench('link to current path', () => {
178 const router = renderRouter(
179 Array.from({ length: numberOfLinks }).map((_, i) => (
180 <Link from="/" search={{ param: i }}>
181 {i}
182 </Link>
183 )),
184 )
185 render(() => <RouterProvider router={router} />)
186 }).run({
187 warmupIterations: 1,
188 })
189 })
190})