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

path-decoder.bench.ts

66 lines1.9 KB

1import { describe, expect, test } from 'vitest'
2import { createMemoryHistory } from '@tanstack/history'
3import { BaseRootRoute, BaseRoute } from '../src'
4import { createTestRouter } from './routerTestUtils'
5​
6describe.each(['stable', 'new-router', 'no-update'] as const)(
7 'path decoder: %s configuration',
8 (mode) => {
9 const makeRouter = (allowed: ReadonlyArray<'@' | '+'>) => {
10 const root = new BaseRootRoute({})
11 const item = new BaseRoute({
12 getParentRoute: () => root,
13 path: '/items/$id',
14 })
15 const router = createTestRouter({
16 routeTree: root.addChildren([item]),
17 history: createMemoryHistory({ initialEntries: ['/'] }),
18 pathParamsAllowedCharacters: allowed,
19 scrollRestoration: false,
20 })
21 router.history.destroy()
22 return router
23 }
24 let router = makeRouter(['@'])
25 const inputs = Array.from({ length: 200 }, (_, index) => ({
26 to: '/items/$id',
27 params: { id: `item-${index % 40}@+` },
28 }))
29 let count = 0
30 let lastHref = ''
31 const run = () => {
32 count++
33 if (mode === 'new-router') {
34 router = makeRouter(count % 2 === 0 ? ['+'] : ['@'])
35 } else if (mode === 'stable') {
36 router.update({
37 ...router.options,
38 context: { count },
39 })
40 }
41 for (const input of inputs) {
42 lastHref = router.buildLocation(input).href
43 }
44 }
45 const verify = () => {
46 expect(lastHref).toBe(
47 mode === 'new-router' && count % 2 === 0
48 ? '/items/item-39%40+'
49 : '/items/item-39@%2B',
50 )
51 }
52 run()
53 verify()
54 test('update and build 200 locations', async ({ bench }) => {
55 await bench(
56 'update and build 200 locations',
57 { afterAll: verify },
58 run,
59 ).run({
60 time: 1500,
61 warmupTime: 500,
62 throws: true,
63 })
64 })
65 },