bench-includes-init.mts
This file
Path
benchmarks/routers/src/bench-includes-init.mts
123 lines 路 3.5 KiB 路 TypeScript
Last changed by
Not known.
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124import 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({ paths: routes.map((r) => r.path),})
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()