Skip to content

CountMinSketch

Defined in: packages/distillate/src/countmin/countmin.ts:105

A Count-Min sketch: a fixed-size frequency estimate with a tunable overestimate and no underestimate.

const sketch = CountMinSketch.create(0.001, 0.001);
sketch.add("alice");
sketch.add("alice", 3);
sketch.count("alice"); // 4 (or a rare overestimate)

new CountMinSketch(__namedParameters): CountMinSketch

Defined in: packages/distillate/src/countmin/countmin.ts:218

Constructs a sketch from low-level CountMinParams. Prefer CountMinSketch.create unless restoring a specific geometry.

CountMinParams

CountMinSketch

get delta(): number

Defined in: packages/distillate/src/countmin/countmin.ts:273

Probability the error bound is exceeded, e ** -depth. At or below the delta passed to CountMinSketch.create, for the same reason.

number


get depth(): number

Defined in: packages/distillate/src/countmin/countmin.ts:244

Number of rows, one probe each.

number


get epsilon(): number

Defined in: packages/distillate/src/countmin/countmin.ts:265

Error factor the geometry implements, e / width.

Sizing rounds width up, so this is at or below the epsilon passed to CountMinSketch.create: the sketch reports what it delivers rather than what was asked for.

number


get seed(): number

Defined in: packages/distillate/src/countmin/countmin.ts:249

Hash seed.

number


get total(): number

Defined in: packages/distillate/src/countmin/countmin.ts:254

Sum of every count recorded, the denominator of the error bound.

number


get width(): number

Defined in: packages/distillate/src/countmin/countmin.ts:239

Counters per row.

number

add(key, count?): void

Defined in: packages/distillate/src/countmin/countmin.ts:304

Records count occurrences of a key.

BytesLike

The key to record, as a string or bytes.

number = 1

How many occurrences to record; defaults to 1.

void


count(key): number

Defined in: packages/distillate/src/countmin/countmin.ts:464

Estimates how many times a key was added. The estimate is never below the true count and is at most CountMinSketch.error above it.

BytesLike

The key to estimate.

number

The estimated count, 0 for a key the sketch has not seen.


equals(other): boolean

Defined in: packages/distillate/src/countmin/countmin.ts:423

Tests structural equality: true when other serializes to identical bytes, meaning identical geometry, seed and counters.

CountMinSketch

The sketch to compare against.

boolean

true if the two sketches are byte-for-byte identical.


error(): number

Defined in: packages/distillate/src/countmin/countmin.ts:286

The additive error bound right now, epsilon * total. A count is at most this far above the truth, with probability 1 - delta.

It grows with what the sketch has recorded, so an estimate means less as the stream goes on, the way a filter’s rate() rises as it fills.

number

The bound, 0 for an empty sketch.


toBytes(): Uint8Array

Defined in: packages/distillate/src/countmin/countmin.ts:399

Serializes the sketch to a portable little-endian byte layout.

The total is not stored. Under plain increment every row sums to it, so the field would be redundant, and a reader deriving it instead gets an integrity check on the counters for free.

Uint8Array

The serialized sketch, readable by CountMinSketch.fromBytes.


toJSON(): FilterJSON

Defined in: packages/distillate/src/countmin/countmin.ts:443

Serializes the sketch to a JSON-friendly envelope wrapping the base64 of CountMinSketch.toBytes.

FilterJSON

The envelope, readable by CountMinSketch.fromJSON.


union(other): CountMinSketch

Defined in: packages/distillate/src/countmin/countmin.ts:350

Returns a new sketch counting everything both this one and other have recorded, as if one sketch had seen both streams.

Both operands are left untouched.

Unlike the filters, this is not idempotent: a.union(a) doubles every count, because counts add where bits only ever turn on.

CountMinSketch

A sketch built with an identical geometry and seed.

CountMinSketch

A new sketch holding both streams.


static create(epsilon, delta, options?): CountMinSketch

Defined in: packages/distillate/src/countmin/countmin.ts:128

Creates a sketch whose estimate is at most epsilon * total above the true count, with probability 1 - delta.

Unlike a filter, a sketch is sized by the error it targets rather than by how many keys it will see, so no key count is needed.

number

Error factor relative to the total recorded, e.g. 0.001.

number

Probability the bound is exceeded, e.g. 0.001.

CountMinOptions = {}

Optional seed.

CountMinSketch

A new, empty sketch.


static from(keys, epsilon, delta, options?): CountMinSketch

Defined in: packages/distillate/src/countmin/countmin.ts:153

Builds a sketch recording keys, at the target error bound. The ergonomic entry point when the stream is already in hand.

A repeated key is counted once per occurrence, which is the whole point of a frequency sketch. This is the opposite of CuckooFilter.from, where a repeat costs a slot and is therefore dropped.

Iterable<BytesLike>

The keys to record, repeats included.

number

Error factor relative to the total recorded.

number

Probability the bound is exceeded.

CountMinOptions = {}

Optional seed.

CountMinSketch

A new sketch holding every occurrence.


static fromBytes(bytes): CountMinSketch

Defined in: packages/distillate/src/countmin/countmin.ts:174

Restores a sketch from its CountMinSketch.toBytes serialization.

Built from the geometry the frame stores, never from a sizing solve: the constructor takes width and depth directly, so there is nothing to re-derive and nothing to disagree about.

Uint8Array

The serialized sketch.

CountMinSketch

The reconstructed sketch.


static fromJSON(value): CountMinSketch

Defined in: packages/distillate/src/countmin/countmin.ts:453

Restores a sketch from its CountMinSketch.toJSON envelope.

unknown

The JSON envelope.

CountMinSketch

The reconstructed sketch.