HyperLogLog
Defined in: packages/distillate/src/hll/hll.ts:85
A HyperLogLog sketch: estimates how many distinct keys it has seen, in space
fixed by p rather than by the cardinality.
Example
Section titled “Example”const sketch = HyperLogLog.create(0.01);sketch.add("alice");Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new HyperLogLog(
params):HyperLogLog
Defined in: packages/distillate/src/hll/hll.ts:101
Creates a sketch at an explicit precision.
Parameters
Section titled “Parameters”params
Section titled “params”Precision and optional hash seed.
Returns
Section titled “Returns”HyperLogLog
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get p():
number
Defined in: packages/distillate/src/hll/hll.ts:294
Precision: the sketch holds 2 ** p registers.
Returns
Section titled “Returns”number
Get Signature
Section titled “Get Signature”get seed():
number
Defined in: packages/distillate/src/hll/hll.ts:299
Hash seed the sketch was built with.
Returns
Section titled “Returns”number
standardError
Section titled “standardError”Get Signature
Section titled “Get Signature”get standardError():
number
Defined in: packages/distillate/src/hll/hll.ts:304
Analytic relative standard error, 1.04 / sqrt(2 ** p).
Returns
Section titled “Returns”number
Methods
Section titled “Methods”add(
key):void
Defined in: packages/distillate/src/hll/hll.ts:314
Records a key. Adding a key already seen leaves the sketch unchanged, which is what lets it count distinct values without storing them.
Parameters
Section titled “Parameters”BytesLike
The key to record.
Returns
Section titled “Returns”void
count()
Section titled “count()”count():
number
Defined in: packages/distillate/src/hll/hll.ts:449
Counts how many distinct keys have been added. Below a few thousand distinct keys the answer is exact; above that, and once the sketch has gone dense, it estimates within standardError.
Returns
Section titled “Returns”number
The cardinality, a whole number; 0 for an empty sketch.
equals()
Section titled “equals()”equals(
other):boolean
Defined in: packages/distillate/src/hll/hll.ts:431
Whether two sketches hold the same registers at the same precision and seed. Which representation each is in does not enter into it.
Parameters
Section titled “Parameters”HyperLogLog
The sketch to compare against.
Returns
Section titled “Returns”boolean
true if the two would answer every query identically.
toBytes()
Section titled “toBytes()”toBytes():
Uint8Array
Defined in: packages/distillate/src/hll/hll.ts:236
Serializes the sketch to a portable little-endian byte layout.
Returns
Section titled “Returns”Uint8Array
The serialized sketch, readable by HyperLogLog.fromBytes.
toJSON()
Section titled “toJSON()”toJSON():
FilterJSON
Defined in: packages/distillate/src/hll/hll.ts:279
Serializes the sketch to a JSON-safe envelope wrapping HyperLogLog.toBytes.
Returns
Section titled “Returns”The envelope, readable by HyperLogLog.fromJSON.
union()
Section titled “union()”union(
other):HyperLogLog
Defined in: packages/distillate/src/hll/hll.ts:356
Merges two sketches, giving a sketch that counts the keys either has seen.
Both operands are left untouched.
Parameters
Section titled “Parameters”HyperLogLog
The sketch to merge with; must share this one’s seed.
Returns
Section titled “Returns”HyperLogLog
A new sketch holding both key sets.
Throws
Section titled “Throws”ParamError if the seeds differ, since the two would have sent the same key to different registers and the merge would mean nothing.
create()
Section titled “create()”
staticcreate(relativeError):HyperLogLog
Defined in: packages/distillate/src/hll/hll.ts:116
Creates a sketch whose standard error meets relativeError.
Parameters
Section titled “Parameters”relativeError
Section titled “relativeError”number
Target relative error, e.g. 0.01 for 1%.
Returns
Section titled “Returns”HyperLogLog
A new, empty sketch.
from()
Section titled “from()”
staticfrom(keys,relativeError):HyperLogLog
Defined in: packages/distillate/src/hll/hll.ts:131
Creates a sketch counting keys, sized to meet relativeError. The
ergonomic entry point when the key set is already in hand.
Needs no count of the keys first, unlike the filters: a sketch is sized by the error it targets, not by how many keys it will see.
Parameters
Section titled “Parameters”Iterable<BytesLike>
The keys to record.
relativeError
Section titled “relativeError”number
Target relative error, e.g. 0.01 for 1%.
Returns
Section titled “Returns”HyperLogLog
A new sketch holding every key.
fromBytes()
Section titled “fromBytes()”
staticfromBytes(bytes):HyperLogLog
Defined in: packages/distillate/src/hll/hll.ts:143
Restores a sketch from its HyperLogLog.toBytes serialization.
Parameters
Section titled “Parameters”Uint8Array
The serialized sketch.
Returns
Section titled “Returns”HyperLogLog
The reconstructed sketch.
fromJSON()
Section titled “fromJSON()”
staticfromJSON(value):HyperLogLog
Defined in: packages/distillate/src/hll/hll.ts:289
Restores a sketch from its HyperLogLog.toJSON envelope.
Parameters
Section titled “Parameters”unknown
The parsed envelope.
Returns
Section titled “Returns”HyperLogLog
The reconstructed sketch.