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: 1000 }); const testData: Array<[string, Box]> = [ ["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("MapArray get reads", () => { let sum = 0; for (const [key] of ma.entries()) { sum += ma.get(key)!.value; } void sum; }) .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(); console.table(bench.table());