SitePoint16 min readtutorialintermediate
Cutting Node.js Memory Footprint with HyperLogLog and Count-Min Sketch in TypeScript
Summary
A step‑by‑step tutorial showing how to implement HyperLogLog and Count‑Min Sketch in pure TypeScript for Node.js, with a fast MurmurHash3‑x86‑32, memory‑bounded registers, merge support, and a Vitest benchmark suite that runs under `--expose-gc`.
- A 32‑bit MurmurHash3 implementation with reusable TextEncoder and `Math.imul` for speed.
- HyperLogLog class using configurable precision (p = 14 → 16 KB, ~0.8 % error) with correct small‑/large‑range corrections and register‑wise merge.
- Count‑Min Sketch (not shown in excerpt) built on the same hash pair via Kirsch‑Mitzenmacher technique.
- Benchmarks demonstrate sub‑megabyte memory usage versus hundreds of megabytes for native `Set`/`Map` at millions of keys.
Node.js services that ingest millions of events per hour quickly run out of heap when using exact containers. Probabilistic sketches give bounded memory and predictable GC, enabling high‑throughput streaming analytics without scaling containers.
6/10





