distillate
v0.19.3
Published
Probabilistic data structures for JavaScript/TypeScript: blocked Bloom filter, scalable Bloom filter, Cuckoo filter, Binary Fuse filter, xor filter, HyperLogLog, Theta sketch, Count-Min sketch, Top-K, KLL sketch, DDSketch quantile sketch, MinHash, RIBLT.
Maintainers
Keywords
Readme
distillate
Probabilistic data structures for JavaScript and TypeScript: approximate answers with a bounded, tunable error, in a fraction of the space an exact answer needs. Membership filters (Bloom, Cuckoo, Binary Fuse), cardinality (HyperLogLog, Theta), frequency (Count-Min, Top-K), quantiles (KLL, DDSketch), similarity (MinHash) and set reconciliation (RIBLT), with zero dependencies and the right structure per workload.
Pre-release (0.x). The published structures are correct, tested, and benchmarked, but the public API may still change before
1.0. Pin a version if you depend on it.
Why
Each structure trades a bounded error for space fixed by the error you choose, not by the size of the data:
- Have I seen this key? Filters, with a tunable false-positive rate and zero false negatives.
- How many distinct keys? Cardinality sketches, about 0.8% error in 12 KiB whether the count is a thousand or a billion.
- How often, and which keys most? Frequency sketches whose estimates never fall below the truth.
- What is the p99, or what share is under x? Quantile sketches, bounded in value (DDSketch) or in rank (KLL).
- How similar are two sets? MinHash signatures, with an index for near-duplicates.
- Which keys do two peers not share? Set reconciliation, at a cost that follows the difference.
And every structure keeps the same promises:
- Runs anywhere: Node, Bun, Deno, browsers, Cloudflare/Vercel edge.
- Correct: no false negatives, property-tested.
- Small: per-structure subpath imports, zero runtime dependencies.
- Portable: a versioned binary format (
toBytes/fromBytes).
Why these promises, and what the incumbents get wrong: overview.md.
Install
npm install distillate
# or: pnpm add distillate / bun add distillate / deno add npm:distillateRequires Node 22+ (or any modern Bun/Deno/browser/edge runtime).
Runtime support
distillate targets ES2022 with zero runtime dependencies and no eval, so it runs on every modern JavaScript runtime:
- Node.js 22, 24 (LTS and current)
- Bun and Deno
- Browsers and Cloudflare/Vercel edge
Every push runs a CI smoke matrix that imports the built package on Node 22/24, Bun, and Deno, so cross-runtime support is verified, not assumed.
Structures
Each structure ships as its own subpath, so you only bundle what you import.
| Import | Structure | Answers | Use for |
| --------------------- | -------------- | ------------------- | --------------------------------------------------------- |
| distillate/bloom | Classic Bloom | seen this key? | Familiar default, migration from bloom-filters |
| distillate/scalable | Scalable Bloom | seen this key? | Key count unknown or growing; keeps its FPR bound |
| distillate/blocked | Blocked Bloom | seen this key? | Faster lookups and a lower FPR for a small space premium |
| distillate/fuse | Binary Fuse | seen this key? | Static set built once and queried a lot; least space |
| distillate/cuckoo | Cuckoo | seen this key? | Keys come and go; the one filter with delete |
| distillate/hll | HyperLogLog | how many distinct? | Counting distinct users, IPs, or keys in fixed space |
| distillate/countmin | Count-Min | how many times? | Counting events per key in fixed space |
| distillate/topk | Top-K | which are heaviest? | Finding the most frequent keys in fixed space |
| distillate/ddsketch | DDSketch | what is the p99? | Latency and size percentiles, mergeable, in fixed space |
| distillate/minhash | MinHash | how similar? | Near-duplicate documents and set overlap, with an index |
| distillate/riblt | RIBLT | which keys differ? | Syncing two sets that mostly agree, exactly |
| distillate/theta | Theta | how many, in both? | Distinct counts to intersect and subtract across segments |
| distillate/kll | KLL | what rank is x? | Ranks, CDFs and SLO fractions with a rank error bound |
The filters are mutable except Binary Fuse, which is built once from the whole key set. HyperLogLog, Count-Min, Top-K, DDSketch, MinHash and Theta are sketches rather than filters: none reports whether it saw any particular key. HyperLogLog counts distinct keys, Count-Min counts how often each key appeared, Top-K reports which keys appeared most, DDSketch reports quantiles of numbers rather than keys, MinHash estimates how much two sets overlap, and Theta counts distinct keys in a union, intersection or difference of sets. RIBLT is neither: it reconciles two peers' sets, finding exactly the keys each side alone holds.
Classic Bloom (distillate/bloom)
import { BloomFilter } from "distillate/bloom";
const filter = BloomFilter.create(100_000, 0.01); // capacity, target FPR
filter.add("alice");
filter.has("alice"); // true
filter.has("bob"); // false (or a ~1% false positive)
const bytes = filter.toBytes();
const restored = BloomFilter.fromBytes(bytes);Also: union(other) (merge equal-parameter filters), bitsPerKey, and a low-level new BloomFilter({ m, k, seed }).
Scalable Bloom (distillate/scalable)
For when you cannot say how many keys are coming. It grows by opening larger, tighter Bloom stages as keys arrive, and keeps its false-positive rate under the target however many open. Full API and trade-offs: Scalable Bloom guide.
import { ScalableBloomFilter } from "distillate/scalable";
const seen = ScalableBloomFilter.create(10, 0.01); // first stage, target FPR
for (let i = 0; i < 30; i++) seen.add(`key-${String(i)}`);
seen.stages; // 2
seen.has("key-29"); // trueA key already held is not counted again, so duplicates never use up capacity. It costs more bits per key than a Classic Bloom sized for a known n; if you know n, use that.
Also: from(keys, epsilon), union, equals, rate, toBytes / fromBytes, toJSON / fromJSON, and growth / tightening / seed options.
Blocked Bloom (distillate/blocked)
import { BlockedBloomFilter } from "distillate/blocked";
const filter = BlockedBloomFilter.create(100_000, 0.01);
filter.add("alice");
filter.has("alice"); // trueSame surface as Classic Bloom (add / has / union / toBytes / fromBytes / bitsPerKey). Reach for it when lookup throughput matters; prefer Classic when space is tight. Cache-line rationale, speed ratios, and the space penalty: Blocked Bloom guide.
Binary Fuse (distillate/fuse)
A static filter: built once from the full key set, then immutable. The most space-efficient option in the lineup. Bits-per-key, FPR, and query throughput: Binary Fuse guide.
import { BinaryFuse8, BinaryFuse16 } from "distillate/fuse";
const filter = BinaryFuse8.from(["alice", "bob", "carol"]);
filter.has("alice"); // true
filter.size; // 3
filter.bitsPerKey; // 64
// Lower false-positive rate, twice the space:
const precise = BinaryFuse16.from(["alice", "bob", "carol"]);bitsPerKey is the space actually allocated, not the asymptotic figure. Three keys pay 64 bits each because the fingerprint array has a fixed minimum; the ~9 you see quoted is what a filter approaches once n is large (about 9.5 at 100k). Size honestly with fuseBitsPerKey(n, width), or the sizing guide.
Also: toBytes / fromBytes. No add / delete; rebuild from the new set to change membership.
Cuckoo (distillate/cuckoo)
For a set that shrinks as well as grows: the one filter here with delete. Full API and trade-offs: Cuckoo guide.
import { CuckooFilter } from "distillate/cuckoo";
const sessions = CuckooFilter.create(100_000, 0.01);
sessions.add("alice");
sessions.has("alice"); // true
sessions.delete("alice"); // true
sessions.has("alice"); // falseOnly delete keys you added: a key never added can share a fingerprint with one that was, and deleting it removes that one instead. An add into a full filter throws CuckooFullError and leaves the filter unchanged. It is larger than a Classic Bloom at common targets and smaller only at about 0.2% and below, so pick it for delete, not for space.
Also: from(keys, epsilon), equals, rate, count, toBytes / fromBytes, toJSON / fromJSON, cuckooSizing(n, epsilon), and a seed option.
HyperLogLog (distillate/hll)
A sketch, not a filter: it counts distinct keys in space fixed by precision rather than by the answer, and cannot report whether it saw any particular key. 12 KiB counts a thousand distinct keys or a billion, at about 0.8% relative error. Full API and sizing: HyperLogLog guide.
import { HyperLogLog } from "distillate/hll";
const sketch = HyperLogLog.create(0.01); // target relative error
sketch.add("alice");
sketch.add("bob");
sketch.add("alice"); // already counted
sketch.count(); // 2
// Merge per-shard sketches without double-counting the overlap:
const other = HyperLogLog.from(["bob", "carol"], 0.01);
sketch.union(other).count(); // 3Below a few thousand distinct keys the sketch counts rather than estimates, so small answers are exact. It switches to fixed-size registers on its own once that stops paying, with nothing to configure.
Also: equals, toBytes / fromBytes, toJSON / fromJSON, standardError, hllSizing(relativeError), and a low-level new HyperLogLog({ p, seed }).
Count-Min (distillate/countmin)
A sketch, not a filter: it estimates how many times each key appeared, in space fixed by the error you ask for rather than by how many keys arrive. The estimate is never below the truth, and at most epsilon of the total above it. Full API and sizing: Count-Min guide.
import { CountMinSketch } from "distillate/countmin";
const hits = CountMinSketch.create(0.001, 0.001); // error factor, failure probability
hits.add("/login");
hits.add("/login", 4);
hits.add("/signup");
hits.count("/login"); // 5
hits.total; // 6
// Combine per-shard sketches: counts add, exactly as if one sketch saw both streams.
const other = CountMinSketch.from(["/login", "/login"], 0.001, 0.001);
hits.union(other).count("/login"); // 7from counts repeats, unlike CuckooFilter.from which ignores them: for a frequency sketch the repeats are the measurement. A counter that would pass 2^32 - 1 throws CountMinOverflowError rather than wrapping, since a wrapped counter would read as an underestimate.
Also: equals, toBytes / fromBytes, toJSON / fromJSON, error(), epsilon, delta, width, depth, countMinSizing(epsilon, delta), and a low-level new CountMinSketch({ width, depth, seed }).
Top-K (distillate/topk)
A sketch that lists the keys seen most often, in a table fixed by the error you ask for rather than by how many keys arrive. A key it holds never reads below its true count, and any key heavier than error() is guaranteed to be held. Full API and the bound: Top-K guide.
import { TopK } from "distillate/topk";
const routes = TopK.create(0.01); // error factor
routes.add("/login", 5);
routes.add("/signup", 2);
routes.add("/about");
const decoder = new TextDecoder();
routes
.top(2)
.map((e) => decoder.decode(e.key))
.join(); // "/login,/signup"
routes.count("/login"); // 5Keys come back as Uint8Array, exactly as recorded, so a serialized Top-K sketch holds your keys verbatim, unlike every other frame here. union combines sketches soundly but, unlike Count-Min's, not byte for byte as one sketch fed both streams.
Also: equals, toBytes / fromBytes, toJSON / fromJSON, total, error(), epsilon, capacity, topKSizing(epsilon), and a low-level new TopK({ capacity, seed }).
DDSketch (distillate/ddsketch)
A sketch of numbers rather than keys: every quantile it reports is within a relative error of the true value, tails included, and two sketches merge exactly. Full API, mappings, stores and the bound: DDSketch guide.
import { DDSketch } from "distillate/ddsketch";
const latency = DDSketch.create(0.01); // within 1%
for (let ms = 1; ms <= 1000; ms++) latency.add(ms);
latency.quantile(0.99); // ~992
latency.max; // 1000By default it bins with the bitwise mapping Datadog's DDSketch.fast() uses, about 1.75 times as fast as the log mapping, and keeps at most 4096 bins per sign, enough for about 19 orders of magnitude at 1%; at a tighter accuracy, raise maxBins through collapsingLowestStore. distillate/ddsketch/protobuf converts to and from the protobuf message Datadog's libraries exchange.
Also: add(value, count), quantiles, count, sum, min, union, equals, toBytes / fromBytes, toJSON / fromJSON, and the bitwiseMapping, logMapping, linearMapping, quadraticMapping, cubicMapping, denseStore, sparseStore, collapsingLowestStore and collapsingHighestStore factories.
MinHash (distillate/minhash)
A sketch of a set: k slots that estimate how much two sets overlap (their Jaccard similarity), one hash per item, merging exactly. MinHashLSH indexes sketches so a query finds the near-duplicates among many without comparing every pair. Full API, the error bound and the index: MinHash guide.
import { MinHash, MinHashLSH } from "distillate/minhash";
const early = Array.from({ length: 1000 }, (_, i) => `user:${String(i)}`);
const late = Array.from({ length: 1000 }, (_, i) => `user:${String(i + 500)}`);
const a = MinHash.from(early, 0.05); // standard error at most 0.05
const b = MinHash.from(late, 0.05);
a.jaccard(b); // ~0.38
const lsh = new MinHashLSH<string>({ threshold: 0.3, k: a.k });
lsh.insert("early", a);
const found = lsh.query(b);
found.length; // 1
found[0]; // "early"The two sets share 500 of 1,500 users, so the true similarity is 1/3. Candidates from the index are likely matches, not confirmed ones; check each with jaccard where it matters. Sets smaller than k carry about 1.3 times the error.
Also: create, add, union, equals, signature, standardError, toBytes / fromBytes, toJSON / fromJSON, minHashSizing, lshSizing, and the index's remove, has and size.
RIBLT (distillate/riblt)
Set reconciliation between two peers: one encodes its set into an endless stream of 20-byte coded symbols, the other pushes them into a decoder until it decodes, then holds exactly the keys each side alone has. The cost follows the difference, about 27 bytes per differing key when many differ, and nobody estimates its size first. Full API, syncing over fetch or a WebSocket, and the limits: RIBLT guide.
import { RibltDecoder, RibltEncoder } from "distillate/riblt";
const server = RibltEncoder.from(["alice", "bob", "dave"]);
const client = RibltDecoder.from(["alice", "carol"]);
let decoded = false;
for (let sent = 0; !decoded && sent < 1024; sent += 4) {
decoded = client.push(server.take(4));
}
client.local().join(); // "carol"
client.remote().length; // 2take(n) is the next n symbols as one Uint8Array frame to send. remote() reports the other side's keys as 64-bit hashes; that side resolves them with RibltDecoder.hashOf. Keys must be distinct, and both peers must share a seed.
Also: add, stream, decoded, RibltDecoder.hashOf.
Theta (distillate/theta)
A sketch of a set that counts its distinct keys and combines with others by union, intersection and difference, so "how many did A and B" or "A but not B" is answered from stored sketches. It is exact up to k distinct keys and within about 1 / sqrt(k) past that, with bounds that say how far off a count may be. Full API, choosing it over HyperLogLog, and the limits: Theta guide.
import { ThetaSketch } from "distillate/theta";
const monday = ThetaSketch.from(["alice", "bob", "carol"], 0.02);
const tuesday = ThetaSketch.from(["bob", "carol", "dave"], 0.02);
monday.union(tuesday).count(); // 4
monday.intersect(tuesday).count(); // 2
monday.difference(tuesday).count(); // 1Past k keys, lowerBound(sd) and upperBound(sd) bracket the estimate at 1, 2 or 3 standard deviations. Set operations need a shared seed, and keep the smaller k of the two.
Also: create, add, equals, isEstimating, theta, toBytes / fromBytes, toJSON / fromJSON, thetaSizing.
KLL (distillate/kll)
A sketch of numbers whose error is bounded in rank: every rank and quantile it reports is within about 1.33% of the stream's positions at the default k, whatever the values, in memory fixed by k alone. It answers "what share of requests met the SLO?", which DDSketch cannot guarantee; DDSketch stays the choice for extreme tails. Full API, choosing between the two, and the limits: KLL guide.
import { KllSketch } from "distillate/kll";
const latency = new KllSketch({ seed: 1 });
for (let ms = 1; ms <= 1000; ms++) latency.add(ms);
latency.rank(200); // ~0.196: the share at or under 200 ms
latency.quantile(0.99); // ~990
latency.max; // 1000Without a seed, each sketch draws its own, which keeps merged shards independent; pass one when runs must repeat byte for byte. A union keeps the smaller k of the two.
Also: create, add(value, count), quantiles, cdf, pmf, count, min, union, normalizedRankError, equals, toBytes / fromBytes, toJSON / fromJSON, kllSizing.
Performance
Classic Bloom head-to-head at a matched 1% false-positive rate over the same 100k keys, measured by identical code (cross-library harness, node v24.14.1, Apple M5):
| Classic Bloom | bits/key | measured FPR | has throughput |
| -------------- | -------- | ------------ | ---------------- |
| distillate | 9.59 | 1.01% | ~21.8 M ops/s |
| bloom-filters | 9.59 | 0.99% | ~0.29 M ops/s |
Same space, same accuracy, ~75x the lookup throughput of bloom-filters (the package distillate replaces), while hashing UTF-8 bytes with MurmurHash3 so filters stay portable and cross-language readable.
Cardinality is a separate head-to-head, at a matched register count (m = 2 ** p, p = 14, the configuration Redis uses) over the same keys, swept from 1k to 10M distinct:
| p = 14, m = 16384 | distillate | bloom-filters | | -------------------- | --------------- | ---------------- | | rel. error, n=1k | 0.00% | 49.63% | | rel. error, n=100k | 0.82% | 0.78% | | rel. error, n=10M | 0.15% | 0.41% | | serialized, n=10M | 12,314 B binary | 40,247 B JSON | | build 10M keys | 0.63 s | 1,165 s (19 min) | | sustained add, n=10M | ~16 M ops/s | ~9 k ops/s |
Both sketches carry the same theoretical error at this precision (1.04 / sqrt(m), about 0.81%), and once n is well past m both stay inside it. The differences are elsewhere.
Small counts. bloom-filters has no working small-range correction, so below roughly 2.5 * m it is off by about half: counting a few thousand distinct keys in a 16k-register sketch, it answers 504 for 1,000. distillate is exact there because it is still holding sparse entries, not because its estimator is better.
Memory does not grow. From 10k distinct keys to 10M, distillate stays at exactly 12,314 bytes. The incumbent's JSON grows from 32,904 to 40,247 bytes over the same range, because larger register values take more digits to spell out.
Building the sketch. bloom-filters holds a flat 9k adds/sec at every size, so its cost is purely linear: 10.6 seconds for 100k distinct keys, 109 seconds for 1M, and 1,165 seconds for 10M. distillate counts the same 10M in 0.63 seconds, and its rate climbs with n rather than staying flat, reaching about 16 M ops/s once the registers are dense.
These are a point-in-time snapshot on one machine. The full report (blocked/fuse, 1M capacity, the bloomfilter micro-package) and exactly how it is measured live in the apps/bench workspace: RESULTS.md, METHODOLOGY.md.
Docs
Design notes, the structure decision matrix, hashing, and the binary format live in docs/:
- overview: what and why
- choosing a structure: decision matrix and the full lineup
- serialization: the binary format spec
- versioning: SemVer policy and supported-runtime baseline
- architecture, hashing: contributor notes, on GitHub
- API reference: generated from TSDoc at site build time (per entry point)
License
MIT © Akshay
