A TypeScript implementation of the HyperLogLog algorithm for cardinality estimation.
npm install @hazemaltakriti/hyperloglog
import { HyperLogLog } from '@hazemaltakriti/hyperloglog';
// Create a new HyperLogLog instance with default precision (14)
const hll = new HyperLogLog();
// Add elements
hll.add('item1');
hll.add('item2');
hll.add('item3');
// Get the estimated cardinality
const count = hll.count();
console.log(`Estimated unique elements: ${count}`);
new HyperLogLog(precision?: number)
-
precision
: Optional. Number between 4 and 16. Default is 14. Higher precision means better accuracy but more memory usage.
Adds an element to the HyperLogLog counter.
Returns the estimated cardinality (number of unique elements).
# Install dependencies
npm install
# Run tests
npm test
# Build the project
npm run build
This implementation is based on the seminal paper:
- Flajolet, P., Fusy, É., Gandouet, O., & Meunier, F. (2007). HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm. In Proceedings of the 2007 International Conference on Analysis of Algorithms.