forked from honojs/hono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench-includes-init.mts
More file actions
123 lines (120 loc) · 3.48 KB
/
bench-includes-init.mts
File metadata and controls
123 lines (120 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import MedleyRouter from '@medley/router'
import type { HTTPMethod } from 'find-my-way'
import findMyWay from 'find-my-way'
import KoaRouter from 'koa-tree-router'
import { run, bench, group } from 'mitata'
import TrekRouter from 'trek-router'
import { LinearRouter } from '../../../src/router/linear-router/index.ts'
import {
RegExpRouter,
PreparedRegExpRouter,
buildInitParams,
} from '../../../src/router/reg-exp-router/index.ts'
import { TrieRouter } from '../../../src/router/trie-router/index.ts'
import type { Route } from './tool.mts'
import { routes } from './tool.mts'
const preparedParams = buildInitParams({
routes
})
const benchRoutes: (Route & { name: string })[] = [
{
name: 'short static',
method: 'GET',
path: '/user',
},
{
name: 'static with same radix',
method: 'GET',
path: '/user/comments',
},
{
name: 'dynamic route',
method: 'GET',
path: '/user/lookup/username/hey',
},
{
name: 'mixed static dynamic',
method: 'GET',
path: '/event/abcd1234/comments',
},
{
name: 'post',
method: 'POST',
path: '/event/abcd1234/comment',
},
{
name: 'long static',
method: 'GET',
path: '/very/deeply/nested/route/hello/there',
},
{
name: 'wildcard',
method: 'GET',
path: '/static/index.html',
},
]
for (const benchRoute of benchRoutes) {
group(`${benchRoute.method} ${benchRoute.path}`, () => {
bench('RegExpRouter', () => {
const router = new RegExpRouter()
for (const route of routes) {
router.add(route.method, route.path, () => {})
}
router.match(benchRoute.method, benchRoute.path)
})
bench('PreparedRegExpRouter', () => {
const router = new PreparedRegExpRouter(preparedParams[0], preparedParams[1])
for (const route of routes) {
router.add(route.method, route.path, () => {})
}
router.match(benchRoute.method, benchRoute.path)
})
bench('TrieRouter', () => {
const router = new TrieRouter()
for (const route of routes) {
router.add(route.method, route.path, () => {})
}
router.match(benchRoute.method, benchRoute.path)
})
bench('LinearRouter', () => {
const router = new LinearRouter()
for (const route of routes) {
router.add(route.method, route.path, () => {})
}
router.match(benchRoute.method, benchRoute.path)
})
bench('MedleyRouter', () => {
const router = new MedleyRouter()
for (const route of routes) {
const store = router.register(route.path)
store[route.method] = () => {}
}
const match = router.find(benchRoute.path)
match.store[benchRoute.method] // get handler
})
bench('FindMyWay', () => {
const router = findMyWay()
for (const route of routes) {
router.on(route.method as HTTPMethod, route.path, () => {})
}
router.find(benchRoute.method as HTTPMethod, benchRoute.path)
})
bench('KoaTreeRouter', () => {
const router = new KoaRouter()
for (const route of routes) {
router.on(route.method, route.path.replace('*', '*foo'), () => {})
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
router.find(benchRoute.method, benchRoute.path)
})
bench('TrekRouter', () => {
const router = new TrekRouter()
for (const route of routes) {
router.add(route.method, route.path, () => {})
}
router.find(benchRoute.method, benchRoute.path)
})
})
}
await run()