Skip to content

Instantly share code, notes, and snippets.

@caderek
Last active January 20, 2025 21:37
Show Gist options
  • Save caderek/0f2c63a0648b24b5700ebea0439e38fc to your computer and use it in GitHub Desktop.
Save caderek/0f2c63a0648b24b5700ebea0439e38fc to your computer and use it in GitHub Desktop.
HexMap benchmark
import { suite, add, cycle, complete } from "benny"
import {
HexMapFlatObj,
HexMapFlatMap,
HexMapNestedObj,
HexMapNestedArr,
HexMapFlatArr,
HexMapFlatUint16,
} from "./HexMap.ts"
function randomTileId() {
return Math.floor(Math.random() * 256)
}
const MAX_COORD = 100
const coords: [q: number, r: number][] = []
for (let q = -MAX_COORD; q < MAX_COORD; q++) {
for (let r = -MAX_COORD; r < MAX_COORD; r++) {
coords.push([q, r])
}
}
console.log(coords.length)
await suite(
"HexMap.set",
add("HexMap with flat object", () => {
const map = new HexMapFlatObj<number>()
for (const c of coords) {
map.set(c[0], c[1], randomTileId())
}
}),
add("HexMap with flat Map", () => {
const map = new HexMapFlatMap<number>()
for (const c of coords) {
map.set(c[0], c[1], randomTileId())
}
}),
add("HexMap with nested objects", () => {
const map = new HexMapNestedObj<number>()
for (const c of coords) {
map.set(c[0], c[1], randomTileId())
}
}),
add("HexMap with nested arrays", () => {
const map = new HexMapNestedArr<number>(MAX_COORD)
for (const c of coords) {
map.set(c[0], c[1], randomTileId())
}
}),
add("HexMap with flat array", () => {
const map = new HexMapFlatArr<number>(MAX_COORD)
for (const c of coords) {
map.set(c[0], c[1], randomTileId())
}
}),
add("HexMap with flat typed array", () => {
const map = new HexMapFlatUint16(MAX_COORD)
for (const c of coords) {
map.set(c[0], c[1], randomTileId())
}
}),
cycle(),
complete(),
)
const map0 = new HexMapFlatObj<number>()
const map1 = new HexMapFlatMap<number>()
const map2 = new HexMapNestedObj<number>()
const map3 = new HexMapNestedArr<number>(MAX_COORD)
const map4 = new HexMapFlatArr<number>(MAX_COORD)
const map5 = new HexMapFlatUint16(MAX_COORD)
for (const [q, r] of coords) {
map0.set(q, r, randomTileId())
map1.set(q, r, randomTileId())
map2.set(q, r, randomTileId())
map3.set(q, r, randomTileId())
map4.set(q, r, randomTileId())
map5.set(q, r, randomTileId())
}
let r0 = 0
let r1 = 0
let r2 = 0
let r3 = 0
let r4 = 0
let r5 = 0
await suite(
"HexMap.get",
add("HexMap with flat object", () => {
for (const c of coords) {
r0 += map0.get(c[0], c[1]) ?? 0
}
}),
add("HexMap with flat Map", () => {
for (const c of coords) {
r1 += map1.get(c[0], c[1]) ?? 0
}
}),
add("HexMap with nested objects", () => {
for (const c of coords) {
r2 += map2.get(c[0], c[1]) ?? 0
}
}),
add("HexMap with nested arrays", () => {
for (const c of coords) {
r3 += map3.get(c[0], c[1]) ?? 0
}
}),
add("HexMap with flat array", () => {
for (const c of coords) {
r4 += map4.get(c[0], c[1]) ?? 0
}
}),
add("HexMap with flat typed array", () => {
for (const c of coords) {
r5 += map5.get(c[0], c[1]) ?? 0
}
}),
cycle(),
complete(),
)
console.log(r0, r1, r2, r3, r4, r5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment