Fix the 'real' bench to make it real for real

This commit is contained in:
Schluffe
2026-08-27 19:29:39 +02:00
parent 935f9ed9ba
commit 832ed9eeb9
+23 -22
View File
@@ -1,54 +1,55 @@
import { Bench } from "tinybench"; import { Bench } from "tinybench";
import { type Box, box } from "./../engine/lib/box.ts"; import { type Box, box } from "./../engine/lib/box.ts";
import { createDictionary, forEntry } from "./../engine/lib/Dictionary.ts"; import {
createDictionary,
forEntry,
getEntry,
} from "./../engine/lib/Dictionary.ts";
import MapArray from "./../engine/lib/MapArray.ts"; import MapArray from "./../engine/lib/MapArray.ts";
const bench = new Bench({ name: "MapArray get", time: 1000 }); const bench = new Bench({ name: "MapArray get", time: 3000 });
let sink = 0;
const testData: Array<[string, Box<number>]> = [ const testData: Array<[string, Box<number>]> = [];
["foo", box(1)], let i = 999;
["bar", box(2)], while (i-- > 0) {
["lol", box(3)], testData.push([String(i), box(i)]);
["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 ma = new MapArray(structuredClone(testData));
const dict = createDictionary(structuredClone(testData)); const dict = createDictionary(structuredClone(testData));
const map = new Map(structuredClone(testData)); const map = new Map(structuredClone(testData));
bench bench
.add("MapArray get reads", () => { .add("MapArray iter/get reads", () => {
let sum = 0; let sum = 0;
for (const [key] of ma.entries()) { for (const [key] of ma.entries()) {
sum += ma.get(key)!.value; sum += ma.get(key)!.value;
} }
void sum; sink += sum;
}) })
.add("Dictionary get reads", () => { .add("Dictionary iter/get reads", () => {
let sum = 0; let sum = 0;
forEntry(dict, (box) => { forEntry(dict, (_, key) => {
sum += box.value; sum += getEntry(dict, key)!.value;
}); });
void sum; sink += sum;
}) })
.add("Map get reads", () => { .add("Map iter/get reads", () => {
let sum = 0; let sum = 0;
for (const [key] of ma.entries()) { for (const [key] of map.entries()) {
sum += map.get(key)!.value; sum += map.get(key)!.value;
} }
void sum; sink += sum;
}); });
await bench.run(); await bench.run();
console.log("sink (ignore):", sink);
console.table(bench.table()); console.table(bench.table());