Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 83bfb3b point at it.

4.12.23Yusuke Wadabefore adoption83bfb3b

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