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.
Example
Section titled “Example”const sketch = CountMinSketch.create(0.001, 0.001);sketch.add("alice");sketch.add("alice", 3);sketch.count("alice"); // 4 (or a rare overestimate)Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”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.
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”Returns
Section titled “Returns”CountMinSketch
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get depth():
number
Defined in: packages/distillate/src/countmin/countmin.ts:244
Number of rows, one probe each.
Returns
Section titled “Returns”number
epsilon
Section titled “epsilon”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get seed():
number
Defined in: packages/distillate/src/countmin/countmin.ts:249
Hash seed.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get total():
number
Defined in: packages/distillate/src/countmin/countmin.ts:254
Sum of every count recorded, the denominator of the error bound.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get width():
number
Defined in: packages/distillate/src/countmin/countmin.ts:239
Counters per row.
Returns
Section titled “Returns”number
Methods
Section titled “Methods”add(
key,count?):void
Defined in: packages/distillate/src/countmin/countmin.ts:304
Records count occurrences of a key.
Parameters
Section titled “Parameters”BytesLike
The key to record, as a string or bytes.
count?
Section titled “count?”number = 1
How many occurrences to record; defaults to 1.
Returns
Section titled “Returns”void
count()
Section titled “count()”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.
Parameters
Section titled “Parameters”BytesLike
The key to estimate.
Returns
Section titled “Returns”number
The estimated count, 0 for a key the sketch has not seen.
equals()
Section titled “equals()”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.
Parameters
Section titled “Parameters”CountMinSketch
The sketch to compare against.
Returns
Section titled “Returns”boolean
true if the two sketches are byte-for-byte identical.
error()
Section titled “error()”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.
Returns
Section titled “Returns”number
The bound, 0 for an empty sketch.
toBytes()
Section titled “toBytes()”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.
Returns
Section titled “Returns”Uint8Array
The serialized sketch, readable by CountMinSketch.fromBytes.
toJSON()
Section titled “toJSON()”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.
Returns
Section titled “Returns”The envelope, readable by CountMinSketch.fromJSON.
union()
Section titled “union()”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.
Parameters
Section titled “Parameters”CountMinSketch
A sketch built with an identical geometry and seed.
Returns
Section titled “Returns”CountMinSketch
A new sketch holding both streams.
create()
Section titled “create()”
staticcreate(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.
Parameters
Section titled “Parameters”epsilon
Section titled “epsilon”number
Error factor relative to the total recorded, e.g. 0.001.
number
Probability the bound is exceeded, e.g. 0.001.
options?
Section titled “options?”CountMinOptions = {}
Optional seed.
Returns
Section titled “Returns”CountMinSketch
A new, empty sketch.
from()
Section titled “from()”
staticfrom(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.
Parameters
Section titled “Parameters”Iterable<BytesLike>
The keys to record, repeats included.
epsilon
Section titled “epsilon”number
Error factor relative to the total recorded.
number
Probability the bound is exceeded.
options?
Section titled “options?”CountMinOptions = {}
Optional seed.
Returns
Section titled “Returns”CountMinSketch
A new sketch holding every occurrence.
fromBytes()
Section titled “fromBytes()”
staticfromBytes(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.
Parameters
Section titled “Parameters”Uint8Array
The serialized sketch.
Returns
Section titled “Returns”CountMinSketch
The reconstructed sketch.
fromJSON()
Section titled “fromJSON()”
staticfromJSON(value):CountMinSketch
Defined in: packages/distillate/src/countmin/countmin.ts:453
Restores a sketch from its CountMinSketch.toJSON envelope.
Parameters
Section titled “Parameters”unknown
The JSON envelope.
Returns
Section titled “Returns”CountMinSketch
The reconstructed sketch.