-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.ts
More file actions
51 lines (43 loc) · 1.31 KB
/
bench.ts
File metadata and controls
51 lines (43 loc) · 1.31 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
import {run, bench, group} from 'mitata'
import httpNext, {IRouter} from './index'
import httpPrevious from '0http-bun'
const silentErrorHandler = () =>
new Response('Internal Server Error', {status: 500})
function setupRouter(router: any) {
router.use((req: any, next: () => any) => {
return next()
})
router.get('/', () => {
return new Response()
})
router.get('/:id', async (req: {params: Record<string, string>}) => {
return new Response(req.params.id)
})
router.get('/:id/error', () => {
throw new Error('Error')
})
}
function benchRouter(name: string, router: any) {
group(name, () => {
bench('Parameter URL', async () => {
await router.fetch(new Request(new URL('http://localhost/0')))
}).gc('inner')
bench('Not Found URL', async () => {
await router.fetch(new Request(new URL('http://localhost/0/404')))
}).gc('inner')
bench('Error URL', async () => {
await router.fetch(new Request(new URL('http://localhost/0/error')))
}).gc('inner')
})
}
const {router} = httpNext({errorHandler: silentErrorHandler})
setupRouter(router)
const {router: routerPrevious} = httpPrevious({
errorHandler: silentErrorHandler,
})
setupRouter(routerPrevious)
benchRouter('Next Router', router)
benchRouter('Previous Router', routerPrevious)
run({
colors: true,
})