Skip to content

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.

const sketch = HyperLogLog.create(0.01);
sketch.add("alice");

new HyperLogLog(params): HyperLogLog

Defined in: packages/distillate/src/hll/hll.ts:101

Creates a sketch at an explicit precision.

HllParams

Precision and optional hash seed.

HyperLogLog

get p(): number

Defined in: packages/distillate/src/hll/hll.ts:294

Precision: the sketch holds 2 ** p registers.

number


get seed(): number

Defined in: packages/distillate/src/hll/hll.ts:299

Hash seed the sketch was built with.

number


get standardError(): number

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

Analytic relative standard error, 1.04 / sqrt(2 ** p).

number

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.

BytesLike

The key to record.

void


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.

number

The cardinality, a whole number; 0 for an empty sketch.


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.

HyperLogLog

The sketch to compare against.

boolean

true if the two would answer every query identically.


toBytes(): Uint8Array

Defined in: packages/distillate/src/hll/hll.ts:236

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

Uint8Array

The serialized sketch, readable by HyperLogLog.fromBytes.


toJSON(): FilterJSON

Defined in: packages/distillate/src/hll/hll.ts:279

Serializes the sketch to a JSON-safe envelope wrapping HyperLogLog.toBytes.

FilterJSON

The envelope, readable by HyperLogLog.fromJSON.


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.

HyperLogLog

The sketch to merge with; must share this one’s seed.

HyperLogLog

A new sketch holding both key sets.

ParamError if the seeds differ, since the two would have sent the same key to different registers and the merge would mean nothing.


static create(relativeError): HyperLogLog

Defined in: packages/distillate/src/hll/hll.ts:116

Creates a sketch whose standard error meets relativeError.

number

Target relative error, e.g. 0.01 for 1%.

HyperLogLog

A new, empty sketch.


static from(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.

Iterable<BytesLike>

The keys to record.

number

Target relative error, e.g. 0.01 for 1%.

HyperLogLog

A new sketch holding every key.


static fromBytes(bytes): HyperLogLog

Defined in: packages/distillate/src/hll/hll.ts:143

Restores a sketch from its HyperLogLog.toBytes serialization.

Uint8Array

The serialized sketch.

HyperLogLog

The reconstructed sketch.


static fromJSON(value): HyperLogLog

Defined in: packages/distillate/src/hll/hll.ts:289

Restores a sketch from its HyperLogLog.toJSON envelope.

unknown

The parsed envelope.

HyperLogLog

The reconstructed sketch.