nish/map
Two operations on the global Map that JavaScript's does not have, written so that a program using them still runs unmodified under Node (docs/wp32-map.md §9.2).
import {
getOrInsert,
reserve,
} from "nish/map";
import { reserve, getOrInsert } from "nish/map"
const counts = new Map<string, i32>()
reserve(counts, 64) // room for 64 words before the table grows
for (const w of ["to", "be", "or", "not", "to", "be"]) {
counts.set(w, getOrInsert(counts, w, 0) + 1) // a new word starts at 0
}
counts.get("to") // 2
getOrInsert
The value of key in m; or, when key is missing, value, after setting key to it.
reserve
Make room in _m for _n entries in all, so that inserting up to _n never grows the table.
The bodies below are the meaning, and they are what runs under Node:
reserve does nothing, and getOrInsert is a get, and a set when the
key was missing. Natively neither body is ever called. The compiler
lowers every call in place, as it lowers hashKey and sameKey: reserve
is the table's reserveSlots, which grows the buckets so that n entries
fit without a rebuild, and getOrInsert is one probe of the key, then the
value it found or an insert through the empty bucket the probe stopped at,
with the hash it already computed. So this module writes no .ll of its own,
and a one-file program that imports it is still one module.
Functions
getOrInsert
The value of key in m; or, when key is missing, value, after
setting key to it. One hash and one probe natively, whichever it was.
value is evaluated either way, as every argument is.
const getOrInsert: <K, V>(
m: Map<K, V>,
key: K,
value: V,
) => V
mMap<K, V>requiredthe map to look in, and to add to when key is missing.
keyKrequiredthe key to find.
valueVrequiredthe value to store when key is missing.
returnsVthe value key already had, or value when it had none.
reserve
Make room in _m for _n entries in all, so that inserting up to _n never
grows the table. Only the speed of a program depends on it: natively it
presizes the bucket table, and under Node, which has no such call, it does
nothing. A count that is not positive does nothing either way.
const reserve: <K, V>(m: Map<K, V>, n: number) => void
mMap<K, V>requiredthe map to presize.
nnumberrequiredhow many entries it should hold, those already in it counted, without growing.

