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