import { Bench } from "tinybench"; import { type Box, box } from "./../engine/lib/box.ts"; import { createDictionary, forEntry, getEntry, } from "./../engine/lib/Dictionary.ts"; import MapArray from "./../engine/lib/MapArray.ts"; const bench = new Bench({ name: "MapArray get", time: 3000 }); let sink = 0; const testData: Array<[string, Box]> = []; let i = 999; while (i-- > 0) { testData.push([String(i), box(i)]); } const ma = new MapArray(structuredClone(testData)); const dict = createDictionary(structuredClone(testData)); const map = new Map(structuredClone(testData)); bench .add("MapArray iter/get reads", () => { let sum = 0; for (const [key] of ma.entries()) { sum += ma.get(key)!.value; } sink += sum; }) .add("Dictionary iter/get reads", () => { let sum = 0; forEntry(dict, (_, key) => { sum += getEntry(dict, key)!.value; }); sink += sum; }) .add("Map iter/get reads", () => { let sum = 0; for (const [key] of map.entries()) { sum += map.get(key)!.value; } sink += sum; }); await bench.run(); console.log("sink (ignore):", sink); console.table(bench.table());