Add a real bench

This commit is contained in:
Schluffe
2026-08-27 18:44:55 +02:00
parent 04e4e91a21
commit 935f9ed9ba
+45 -5
View File
@@ -1,13 +1,53 @@
import { Bench } from "tinybench";
import { type Box, box } from "./../engine/lib/box.ts";
import { createDictionary, forEntry } from "./../engine/lib/Dictionary.ts";
import MapArray from "./../engine/lib/MapArray.ts";
const bench = new Bench({ name: "MapArray get", time: 300 });
const bench = new Bench({ name: "MapArray get", time: 1000 });
const testData: Array<[string, Box<number>]> = [
["foo", box(1)],
["bar", box(2)],
["lol", box(3)],
["bob", box(4)],
["zig", box(5)],
["fuzz", box(6)],
["buss", box(7)],
["grep", box(8)],
["zeap", box(9)],
];
const ma = new MapArray(structuredClone(testData));
const dict = createDictionary(structuredClone(testData));
const map = new Map(structuredClone(testData));
bench
.add("foo", () => {
console.log("foo");
.add("MapArray get reads", () => {
let sum = 0;
for (const [key] of ma.entries()) {
sum += ma.get(key)!.value;
}
void sum;
})
.add("bar", () => {
console.log("bar");
.add("Dictionary get reads", () => {
let sum = 0;
forEntry(dict, (box) => {
sum += box.value;
});
void sum;
})
.add("Map get reads", () => {
let sum = 0;
for (const [key] of ma.entries()) {
sum += map.get(key)!.value;
}
void sum;
});
await bench.run();