Skip to content

TopK

Defined in: packages/data/src/probabilistic/top-k.ts:39

Approximate heavy hitters: the k most frequent items in a stream.

A CountMinSketch estimates every item’s frequency in fixed space; on its own it cannot say which items are frequent, because it never stores them. This pairs it with a bounded set of candidates — the k items with the highest estimates seen so far — which is what turns “how often was X seen” into “what is seen most”.

Both approximations compound: an item is reported with the sketch’s over-estimate, and a genuinely heavy item can be missed if it was evicted from the candidate set early, before its count grew. Raising k above the number actually wanted makes that far less likely.

Time complexity: O(depth + k) per add — the sketch update plus a scan of the candidates. heavyHitters is O(k log k) to sort.

T

The item type.

new TopK<T>(options): TopK<T>

Defined in: packages/data/src/probabilistic/top-k.ts:47

TopKOptions<T>

TopK<T>

readonly k: number

Defined in: packages/data/src/probabilistic/top-k.ts:45

How many heavy hitters are tracked.

add(item, times?): void

Defined in: packages/data/src/probabilistic/top-k.ts:72

O(depth + k).

T

number = 1

void


addAll(items, times?): void

Defined in: packages/data/src/probabilistic/top-k.ts:98

O(m·(depth + k)) in the number of items.

Iterable<T>

number = 1

void


clear(): void

Defined in: packages/data/src/probabilistic/top-k.ts:66

void


count(): number

Defined in: packages/data/src/probabilistic/top-k.ts:62

How many candidates are currently tracked, at most k.

number


estimate(item): number

Defined in: packages/data/src/probabilistic/top-k.ts:105

The sketch’s estimate for item, tracked or not. O(depth).

T

number


heavyHitters(): HeavyHitter<T>[]

Defined in: packages/data/src/probabilistic/top-k.ts:120

The tracked heavy hitters, most frequent first. O(k log k).

Counts are re-read from the sketch, so they are current rather than whatever they were when the candidate was admitted.

HeavyHitter<T>[]


isTracked(item): boolean

Defined in: packages/data/src/probabilistic/top-k.ts:110

Whether item is currently one of the tracked candidates. O(1).

T

boolean


total(): number

Defined in: packages/data/src/probabilistic/top-k.ts:57

The total of every count added, exact.

number