flaska/benchmark/strings.mjs

121 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2024-11-02 12:44:54 +00:00
import { buildTree, compileTreeIntoIfs, compileTreeIntoIfsWithBuffer } from "./compiler/compiler.mjs"
import { summary, run, bench } from 'mitata';
import { printCurrentStatus, printStatusHelperText } from "./compiler/utils.mjs";
import { FlaskaRouter } from "../flaska.mjs";
// Warmup (de-optimize `bench()` calls)
bench('noop', () => { });
bench('noop2', () => { });
let paths = [
'/fsdafasfa',
'/ymreklhmse',
'/h34nmlaeknmgl',
'/asdgsdagas',
'/ahaewhweaaa',
'/adshashaea',
'/sdafasfsadfasdfas',
//'/gdfsfgsfdsgsdrgsregsergsregersgserersgsergersg',
//'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
]
let pathsBla = [
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
'/test5afdlkasdflksad flsakdf lsakf asdlkfa lsdkfasdlkdfalklk',
]
let pathsBuffer = paths.map(x => new Uint8Array(Buffer.from(x)))
let tree = buildTree(paths)
let noop = function() { }
const ifTree = compileTreeIntoIfs(tree)
// console.log(ifTree.toString())
const ifTreeBuffer = compileTreeIntoIfsWithBuffer(tree)
// console.log(ifTreeBuffer.toString())
const flaskaRouter = new FlaskaRouter()
for (let path of paths) {
flaskaRouter.addRoute(path, noop)
}
const m = new Map(paths.map(x => [x, x]))
function mapFlat(str) {
return m.get(str) ?? null
}
function toBuffer(str) {
return Buffer.from(str)
}
function toUint(str) {
return new Uint8Array(Buffer.from(str))
}
function toManualArray(str) {
let length = str.length
let out = new Array(length)
for (let i = 0; i < length; i++) {
out[i] = str.charCodeAt(i)
}
return out
}
function allocBufferUnsafe(str) {
return Buffer.allocUnsafe(str.length)
}
function allocBufferSafe(str) {
return Buffer.alloc(str.length)
}
function allocUint8(str) {
return new Uint8Array(str.length)
}
function toManualArraySplitMap(str) {
return str.split('').map(x => x.charCodeAt(0))
}
let func1 = [mapFlat, toBuffer, toUint, toManualArray, toManualArraySplitMap, allocBufferUnsafe, allocBufferSafe, allocUint8, ifTree];
for (var i = 0; i < 100000; i++) {
for (let fun of func1) {
paths.map(fun)
}
}
for (var i = 0; i < 100000; i++) {
for (let path of paths) {
flaskaRouter.match(path)
}
}
for (let fun of func1) {
printCurrentStatus(fun);
}
printCurrentStatus(flaskaRouter.match)
printStatusHelperText()
summary(() => {
bench('if tree', function() {
return paths.map(ifTree)
})
bench('if tree buffer edition', function() {
return paths.map(ifTreeBuffer)
});
bench('flaskarouter', function() {
return paths.map(flaskaRouter.match.bind(flaskaRouter))
});
/*bench('map edition', function() {
return paths.map(mapFlat)
});
bench('if tree pre buffer edition', function() {
return pathsBuffer.map(ifTreeBuffer)
});
bench('toBuffer', () => pathsBla.map(toBuffer));
bench('toUint', () => pathsBla.map(toUint));
bench('toManualArraySplitMap', () => pathsBla.map(toManualArraySplitMap))
bench('toManualArray', () => pathsBla.map(toManualArray))*/
bench('allocBufferUnsafe', () => pathsBla.map(allocBufferUnsafe))
bench('allocBufferSafe', () => pathsBla.map(allocBufferSafe))
bench('allocUint8', () => pathsBla.map(allocUint8))
})
run();