flaska/benchmark/map_query.mjs

107 lines
3.3 KiB
JavaScript
Raw Normal View History

2024-11-02 12:44:54 +00:00
import { summary, run, bench } from 'mitata';
function printCurrentStatus(fn) {
let opt = %GetOptimizationStatus(fn)
console.log(`${opt.toString(2).padStart(17, '0').split('').join(' ')} (${opt}) ${fn.name}`)
}
function printStatusHelperText() {
console.log(`┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬
is function
is never optimized
is always optimized
is maybe deoptimized
is optimized
is optimized by TurboFan
is interpreted
is marked for optimization
is marked for concurrent optimization
is optimizing concurrently
is executing
topmost frame is turbo fanned
lite mode
marked for deoptimization
baseline
topmost frame is interpreted
topmost frame is baseline`)
}
// Warmup (de-optimize `bench()` calls)
bench('noop', () => { });
bench('noop2', () => { });
function mapGetAndCheck(map, t) {
let x = map.get(t)
if (x) return x
return t
}
function mapCheckBeforeGet(map, t) {
if (map.has(t)) {
return map.get(t)
}
return t
}
let paths = [
'a',
'aa',
'aaa',
'aaaa',
'aaaaa',
'aaaaaa',
'aaaaaaa',
'aaaaaaaa',
]
let tests = [
'a',
'aa',
'aaa',
'aaaa',
'aaaaa',
'aaaaaa',
'aaaaaaa',
'aaaaaaaa',
'b',
'bb',
'bbb',
'bbbb',
'bbbbb',
'bbbbbb',
'bbbbbbb',
'bbbbbbbb',
'c',
'cc',
'ccc',
'cccc',
'ccccc',
'cccccc',
'ccccccc',
'cccccccc',
]
let map = new Map(paths.map(x => [x, { a: x }]))
let func1 = [mapGetAndCheck, mapCheckBeforeGet];
for (let fun of func1) {
console.log('-- begin', fun.name)
for (var i = 0; i < 1000000; i++) {
tests.map(t => fun(map, t))
}
printCurrentStatus(fun);
}
printStatusHelperText()
summary(() => {
func1.forEach(function(fun) {
bench(fun.name, function() {
return paths.map(t => fun(map, t))
})
})
})
run().then(function() {
for (let fun of func1) {
printCurrentStatus(fun);
}
printStatusHelperText()
});