flaska/benchmark/mapl_router_compile.mjs

65 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-10-27 09:46:01 +00:00
import { createRouter, insertItem } from '@mapl/router'
import { compileRouter } from './mapl_compiler.mjs'
import * as consts from './const.js'
let testPaths = consts.allManyRoutes.map(x => x.replace(/:[^\/]+/g, '*')) // consts.allManyRoutes
testPaths = [
'/*/file',
'/*',
]
const router2 = createRouter();
for (let route of testPaths) {
insertItem(router2, route, '{works}')
}
const match2 = compileRouter(router2)
for (let route of testPaths) {
let check = route.replace(/\*/g, 'something')
let gotMatched = match2(check.slice(1))
console.log(check, gotMatched?.[0] || '{ERROR NO MATCH}')
}
process.exit(0)
const r = createRouter();
for (let route of testPaths) {
let cleaned = route.replace(/:[^\/]+/g, '*')
insertItem(r, cleaned, () => { })
}
const m = compileRouter(r)
for (let item of testPaths) {
console.log(item, m(item.slice(1)))
}
function printTime (t) {
let time = Number(t)
let units = ['n', 'μ', 'm', 'c', 's']
let unit = units[0]
let unitPower = 1
for (let i = 0; i < units.length; i++) {
let power = Math.pow(10, (i + 1) * 3)
if (power * 2 > time) {
break
}
unitPower = power
unit = units[1]
}
console.log(t, '=', Number((time / unitPower).toFixed(2)), unit)
}
let paths = testPaths.map(x => ({ path: x }))
let s1 = process.hrtime.bigint()
let s2 = process.hrtime.bigint()
const match = compileRouter(router);
console.log(match.toString());
let s3 = process.hrtime.bigint()
let time = s3 - s2 - (s2 - s1)
printTime(time)