Skip to content

knotkit/frequency-table

Maintains exact counts, prefix sums, range sums, and rank selection over a fixed integer domain

import {
function fenwickFrequencyTable(capacity: number): FrequencyTable

Create an empty binary indexed tree. Count, add, sum and selection take O(log(capacity + 1)). Construction and clearing take O(capacity + 1) and counters occupy 4(capacity + 1) bytes.

@throws ― {FrequencyTableInvalidInputError} INVALID_CAPACITY unless capacity is an integer in [0, 2^31 - 1].

fenwickFrequencyTable
} from 'knotkit/frequency-table'
const
const counts: FrequencyTable
counts
=
function fenwickFrequencyTable(capacity: number): FrequencyTable

Create an empty binary indexed tree. Count, add, sum and selection take O(log(capacity + 1)). Construction and clearing take O(capacity + 1) and counters occupy 4(capacity + 1) bytes.

@throws ― {FrequencyTableInvalidInputError} INVALID_CAPACITY unless capacity is an integer in [0, 2^31 - 1].

fenwickFrequencyTable
(10)
const counts: FrequencyTable
counts
.
FrequencyTable.add: (key: number, delta?: number) => void

Add delta occurrences of key, defaulting to one. Negative deltas remove occurrences.

@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for an invalid key, INVALID_DELTA for a delta that is not a safe integer, COUNT_UNDERFLOW for a negative resulting frequency, or COUNT_OVERFLOW for a total above 2^32 - 1. A rejected update leaves all counts unchanged.

add
(2, 2)
const counts: FrequencyTable
counts
.
FrequencyTable.add: (key: number, delta?: number) => void

Add delta occurrences of key, defaulting to one. Negative deltas remove occurrences.

@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for an invalid key, INVALID_DELTA for a delta that is not a safe integer, COUNT_UNDERFLOW for a negative resulting frequency, or COUNT_OVERFLOW for a total above 2^32 - 1. A rejected update leaves all counts unchanged.

add
(7)
const counts: FrequencyTable
counts
.
FrequencyTable.add: (key: number, delta?: number) => void

Add delta occurrences of key, defaulting to one. Negative deltas remove occurrences.

@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for an invalid key, INVALID_DELTA for a delta that is not a safe integer, COUNT_UNDERFLOW for a negative resulting frequency, or COUNT_OVERFLOW for a total above 2^32 - 1. A rejected update leaves all counts unchanged.

add
(9)
const counts: FrequencyTable
counts
.
FrequencyTable.count: (key: number) => number

Read the exact frequency of key. An unobserved key has count zero.

@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for a key outside the integer domain.

count
(2) // 2
// Sorted observations are [2, 2, 7, 9].
const counts: FrequencyTable
counts
.
FrequencyTable.at: (position: number) => number

Read the key at a zero-based position, counting duplicates. Negative positions count from the end.

@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for a noninteger position or one outside [-size, size).

at
(
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.floor(x: number): number

Returns the greatest integer less than or equal to its numeric argument.

@param ― x A numeric expression.

floor
((
const counts: FrequencyTable
counts
.
FrequencyTable.size: number

Sum of all frequencies, including repeated occurrences. O(1).

size
- 1) / 2)) // 2, the lower median
const counts: FrequencyTable
counts
.
FrequencyTable.rangeSum: (start: number, end: number) => number

Sum frequencies of keys in [start, end).

@throws ― {FrequencyTableInvalidInputError} INVALID_RANGE for noninteger, reversed, or out-of-domain bounds.

rangeSum
(2, 8) // 3
const counts: FrequencyTable
counts
.
FrequencyTable.add: (key: number, delta?: number) => void

Add delta occurrences of key, defaulting to one. Negative deltas remove occurrences.

@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for an invalid key, INVALID_DELTA for a delta that is not a safe integer, COUNT_UNDERFLOW for a negative resulting frequency, or COUNT_OVERFLOW for a total above 2^32 - 1. A rejected update leaves all counts unchanged.

add
(2, -1)
const counts: FrequencyTable
counts
.
FrequencyTable.at: (position: number) => number

Read the key at a zero-based position, counting duplicates. Negative positions count from the end.

@throws ― {FrequencyTableInvalidInputError} INVALID_INDEX for a noninteger position or one outside [-size, size).

at
(-1) // 9, the largest observed key
frequency-table.ts
  • read the exact number of occurrences of a key
  • track repeated integer observations as items enter and leave
  • sum frequencies between two bounds after updates
  • read a median or quantile by its position among all observations
FactoryChoose whenCost
fenwickFrequencyTable
kind fenwick
prefix sums or rank selections repeatedly span many keys between updatesfour bytes per addressable key and one sentinel counter, with at most 2^32 - 1 occurrences
flatFrequencyTable
kind flat
the domain has few keys or the workload primarily updates and reads individual countsfour bytes per addressable key; selection scans from zero to the selected key

Performance and benchmarks for this module

{"v":1,"box":[70.1337,94.1597,923.5333,444.3555],"x":{"s":"log","lim":[5.85634278378,11190.6017833],"inv":false,"g":0,"t":"Capacity (keys)","tk":[[8,"8"],[64,"64"],[512,"512"],[8192,"8,192"]],"b":10},"y":{"s":"log","lim":[0.8,10240],"inv":false,"g":0,"tk":[[1,"1"],[10,"10"],[100,"100"],[1000,"1,000"],[8192,"8,192"]],"b":10},"ti":"Longest prefix: Fenwick reads 13 counters, flat 8,191","a":[{"k":"line","c":"#fffff8","s":"x","d":[8,7,16,15,32,31,64,63,128,127,256,255,512,511,1024,1023,2048,2047,4096,4095,8192,8191],"p":[105.37,364.04,183.66,335.81,261.95,308.93,340.25,282.67,418.54,256.71,496.83,230.9,575.13,205.16,653.42,179.46,731.71,153.77,810.01,128.1,888.3,102.43]},{"k":"line","c":"#c4501b","s":"x","d":[8,7,16,15,32,31,64,63,128,127,256,255,512,511,1024,1023,2048,2047,4096,4095,8192,8191],"p":[105.37,364.04,183.66,335.81,261.95,308.93,340.25,282.67,418.54,256.71,496.83,230.9,575.13,205.16,653.42,179.46,731.71,153.77,810.01,128.1,888.3,102.43]},{"k":"line","c":"#fffff8","s":"x","d":[8,3,16,4,32,5,64,6,128,7,256,8,512,9,1024,10,2048,11,4096,12,8192,13],"p":[105.37,395.41,183.66,384.76,261.95,376.5,340.25,369.74,418.54,364.04,496.83,359.09,575.13,354.73,653.42,350.83,731.71,347.3,810.01,344.08,888.3,341.11]},{"k":"line","c":"#1d5fa6","s":"x","d":[8,3,16,4,32,5,64,6,128,7,256,8,512,9,1024,10,2048,11,4096,12,8192,13],"p":[105.37,395.41,183.66,384.76,261.95,376.5,340.25,369.74,418.54,364.04,496.83,359.09,575.13,354.73,653.42,350.83,731.71,347.3,810.01,344.08,888.3,341.11]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 8 64 512 8,192 Capacity (keys) 1 10 100 1,000 8,192 Counters read for prefixSum(capacity − 1) (model) Flat: 8,191 Fenwick: 13 Longest prefix: Fenwick reads 13 counters, flat 8,191 {"v":1,"box":[70.1337,605.1597,923.5333,955.3555],"x":{"s":"log","lim":[5.85634278378,11190.6017833],"inv":false,"g":1,"t":"Capacity (keys)","tk":[[8,"8"],[64,"64"],[512,"512"],[8192,"8,192"]],"b":10},"y":{"s":"log","lim":[0.8,10240],"inv":false,"g":1,"tk":[[1,"1"],[10,"10"],[100,"100"],[1000,"1,000"],[8192,"8,192"]],"b":10},"ti":"One update: Fenwick writes 14 counters, flat 1","a":[{"k":"line","c":"#fffff8","s":"x","d":[8,1,16,1,32,1,64,1,128,1,256,1,512,1,1024,1,2048,1,4096,1,8192,1],"p":[105.37,947.09,183.66,947.09,261.95,947.09,340.25,947.09,418.54,947.09,496.83,947.09,575.13,947.09,653.42,947.09,731.71,947.09,810.01,947.09,888.3,947.09]},{"k":"line","c":"#c4501b","s":"x","d":[8,1,16,1,32,1,64,1,128,1,256,1,512,1,1024,1,2048,1,4096,1,8192,1],"p":[105.37,947.09,183.66,947.09,261.95,947.09,340.25,947.09,418.54,947.09,496.83,947.09,575.13,947.09,653.42,947.09,731.71,947.09,810.01,947.09,888.3,947.09]},{"k":"line","c":"#fffff8","s":"x","d":[8,4,16,5,32,6,64,7,128,8,256,9,512,10,1024,11,2048,12,4096,13,8192,14],"p":[105.37,895.76,183.66,887.5,261.95,880.74,340.25,875.04,418.54,870.09,496.83,865.73,575.13,861.83,653.42,858.3,731.71,855.08,810.01,852.11,888.3,849.37]},{"k":"line","c":"#1d5fa6","s":"x","d":[8,4,16,5,32,6,64,7,128,8,256,9,512,10,1024,11,2048,12,4096,13,8192,14],"p":[105.37,895.76,183.66,887.5,261.95,880.74,340.25,875.04,418.54,870.09,496.83,865.73,575.13,861.83,653.42,858.3,731.71,855.08,810.01,852.11,888.3,849.37]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 8 64 512 8,192 Capacity (keys) 1 10 100 1,000 8,192 Counters written for add(0) (model) Flat: 1 Fenwick: 14 One update: Fenwick writes 14 counters, flat 1
{"v":1,"box":[70.1337,122.3921,475.1175,444.3555],"x":{"s":"log","lim":[5.85634278378,11190.6017833],"inv":false,"g":0,"t":"Capacity (keys)","tk":[[8,"8"],[64,"64"],[512,"512"],[8192,"8,192"]],"b":10},"y":{"s":"log","lim":[0.8,10240],"inv":false,"g":0,"tk":[[1,"1"],[10,"10"],[100,"100"],[1000,"1,000"],[8192,"8,192"]],"b":10},"ti":"Longest prefix: Fenwick reads 13\ncounters, flat 8,191","a":[{"k":"line","c":"#fffff8","s":"x","d":[8,7,16,15,32,31,64,63,128,127,256,255,512,511,1024,1023,2048,2047,4096,4095,8192,8191],"p":[86.85,370.51,124.01,344.57,161.16,319.85,198.32,295.71,235.47,271.84,272.63,248.11,309.78,224.45,346.93,200.82,384.09,177.2,421.24,153.59,458.4,129.99]},{"k":"line","c":"#c4501b","s":"x","d":[8,7,16,15,32,31,64,63,128,127,256,255,512,511,1024,1023,2048,2047,4096,4095,8192,8191],"p":[86.85,370.51,124.01,344.57,161.16,319.85,198.32,295.71,235.47,271.84,272.63,248.11,309.78,224.45,346.93,200.82,384.09,177.2,421.24,153.59,458.4,129.99]},{"k":"line","c":"#fffff8","s":"x","d":[8,3,16,4,32,5,64,6,128,7,256,8,512,9,1024,10,2048,11,4096,12,8192,13],"p":[86.85,399.36,124.01,389.56,161.16,381.97,198.32,375.76,235.47,370.51,272.63,365.97,309.78,361.96,346.93,358.37,384.09,355.12,421.24,352.16,458.4,349.44]},{"k":"line","c":"#1d5fa6","s":"x","d":[8,3,16,4,32,5,64,6,128,7,256,8,512,9,1024,10,2048,11,4096,12,8192,13],"p":[86.85,399.36,124.01,389.56,161.16,381.97,198.32,375.76,235.47,370.51,272.63,365.97,309.78,361.96,346.93,358.37,384.09,355.12,421.24,352.16,458.4,349.44]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 8 64 512 8,192 Capacity (keys) 1 10 100 1,000 8,192 Counters read for prefixSum(capacity − 1) (model) Flat: 8,191 Fenwick: 13 Longest prefix: Fenwick reads 13 counters, flat 8,191 {"v":1,"box":[70.1337,633.3921,475.1175,955.3555],"x":{"s":"log","lim":[5.85634278378,11190.6017833],"inv":false,"g":1,"t":"Capacity (keys)","tk":[[8,"8"],[64,"64"],[512,"512"],[8192,"8,192"]],"b":10},"y":{"s":"log","lim":[0.8,10240],"inv":false,"g":1,"tk":[[1,"1"],[10,"10"],[100,"100"],[1000,"1,000"],[8192,"8,192"]],"b":10},"ti":"One update: Fenwick writes 14 counters,\nflat 1","a":[{"k":"line","c":"#fffff8","s":"x","d":[8,1,16,1,32,1,64,1,128,1,256,1,512,1,1024,1,2048,1,4096,1,8192,1],"p":[86.85,947.76,124.01,947.76,161.16,947.76,198.32,947.76,235.47,947.76,272.63,947.76,309.78,947.76,346.93,947.76,384.09,947.76,421.24,947.76,458.4,947.76]},{"k":"line","c":"#c4501b","s":"x","d":[8,1,16,1,32,1,64,1,128,1,256,1,512,1,1024,1,2048,1,4096,1,8192,1],"p":[86.85,947.76,124.01,947.76,161.16,947.76,198.32,947.76,235.47,947.76,272.63,947.76,309.78,947.76,346.93,947.76,384.09,947.76,421.24,947.76,458.4,947.76]},{"k":"line","c":"#fffff8","s":"x","d":[8,4,16,5,32,6,64,7,128,8,256,9,512,10,1024,11,2048,12,4096,13,8192,14],"p":[86.85,900.56,124.01,892.97,161.16,886.76,198.32,881.51,235.47,876.97,272.63,872.96,309.78,869.37,346.93,866.12,384.09,863.16,421.24,860.44,458.4,857.91]},{"k":"line","c":"#1d5fa6","s":"x","d":[8,4,16,5,32,6,64,7,128,8,256,9,512,10,1024,11,2048,12,4096,13,8192,14],"p":[86.85,900.56,124.01,892.97,161.16,886.76,198.32,881.51,235.47,876.97,272.63,872.96,309.78,869.37,346.93,866.12,384.09,863.16,421.24,860.44,458.4,857.91]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 8 64 512 8,192 Capacity (keys) 1 10 100 1,000 8,192 Counters written for add(0) (model) Flat: 1 Fenwick: 14 One update: Fenwick writes 14 counters, flat 1
  • fenwick: prefixSum(8191) = 5, count(0) = 3.
  • flat: prefixSum(8191) = 5, count(0) = 3.
  • The flat layout scans the prefix. Fenwick sums one partial counter per set bit in the prefix length.

Hover or focus a chart for exact values. Each change reruns demo.ts against knotkit/frequency-table in your browser; the defaults reproduce the README figure.

ExportReturnsDoes
fenwickFrequencyTableFrequencyTableBinary indexed tree of exact nonnegative integer frequencies.
flatFrequencyTableFrequencyTableOne unsigned counter per key. Range sums and rank selection scan counters.

Cost per member. Open a row for every operation.

fenwickFrequencyTable 7 members
MemberCost
addO(log C)
prefixSumO(log C)
rangeSumO(log C)
atO(log C)
clearO(C)
sizeO(1)
countO(log C)
flatFrequencyTable 7 members
MemberCost
addO(1)
prefixSumO(end + 1)
rangeSumO(end - start + 1)
atO(C)
clearO(C)
sizeO(1)
countO(1)

Every error this module throws is FrequencyTableInvalidInputError, with a code from a closed, exported list. Branch on the code; the message is for people.

INVALID_CAPACITY INVALID_INDEX INVALID_RANGE INVALID_DELTA COUNT_UNDERFLOW COUNT_OVERFLOW
If you need to…Use instead
rank keys under a custom comparator or store payloadsknotkit/ordered-map
membership and set algebra on unique unsigned IDsknotkit/bitset
prefix sums over immutable signed valuesa prefix-sum array
estimate per-key frequencies in a stream with bounded memoryknotkit/scalar-sketch

Measured by the suite in packages/frequency-table/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone. Compare the recorded timings.

Prompt · knotkit/frequency-table
Use knotkit/frequency-table for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/frequency-table'.

Exports:
- `fenwickFrequencyTable` → `FrequencyTable`: Binary indexed tree of exact nonnegative integer frequencies.
- `flatFrequencyTable` → `FrequencyTable`: One unsigned counter per key. Range sums and rank selection scan counters.

Pick the layout by these rules:
- `fenwickFrequencyTable` (kind 'fenwick') when prefix sums or rank selections repeatedly span many keys between updates
- `flatFrequencyTable` (kind 'flat') when the domain has few keys or the workload primarily updates and reads individual counts

Use something else when:
- rank keys under a custom comparator or store payloads → knotkit/ordered-map
- membership and set algebra on unique unsigned IDs → knotkit/bitset
- prefix sums over immutable signed values → a prefix-sum array
- estimate per-key frequencies in a stream with bounded memory → knotkit/scalar-sketch

Errors are FrequencyTableInvalidInputError with a `code` property. Branch on `error.code`, never on the message.

Full page: https://knotkit.jkomyno.dev/modules/frequency-table.md
Types: node_modules/knotkit/frequency-table/ and https://knotkit.jkomyno.dev/reference/knotkit/frequency-table/