Fastest Bloom Filter on NPM. Powered by WASM, written in TypeScript & C.
npm install fast-bloom-filter> The fastest Bloom Filter on npm.
> Powered by WASM, written in TypeScript & C.
---
With n items, m bits and k hash functions the false-positive probability is P_fp ā (1 - e^{-kn/m})^k; using the near-optimal k ā (m/n) ln 2 yields m ā -(n ln p)/(ln 2)^2 bits to hit a target rate p (ā9.6 bits per element for p = 0.01).
``bash`
npm install fast-bloom-filteror
pnpm add fast-bloom-filteror
bun add fast-bloom-filterš ļø Usage
`TypeScript
import FastBloomFilter from "fast-bloom-filter";
// Create a bloom filter for 100 elements with an expected false positive rate of 0.01
const bloomFilter = await FastBloomFilter.createOptimal(100, 0.01);
// Create a bloom filter of 100 bits with 4 hash rounds
//const bloomFilter = await FastBloomFilter.create(100, 4);
bloomFilter.addString("hello");
bloomFilter.add(Buffer.from([0x01, 0x02, 0x03]));
bloomFilter.hasString("hello"); // true
bloomFilter.has(Buffer.from([0x01, 0x02, 0x03])); // true
bloomFilter.hasString("foo"); // likely false
bloomFilter.has(Buffer.from([0x01, 0x02, 0x03, 0x04])); // likely false
const snapshot = bloomFilter.export();
bloomFilter.dispose();
const restored = await FastBloomFilter.import(snapshot);
restored.hasString("hello"); // true
restored.has(Buffer.from([0x01, 0x02, 0x03])); // true
restored.hasString("foo"); // likely false
restored.has(Buffer.from([0x01, 0x02, 0x03, 0x04])); // likely false
restored.dispose();
``
š Burton H. Bloom (1970)
_Space/time trade-offs in hash coding with allowable errors_
š Kirsch and Mitzenmacher (2006)
_Less Hashing, Same Performance: Building a Better Bloom Filter_
Timings are medians of 3 runs with GC before/after each run. For adapters without Buffer support, buffer datasets are base64-encoded strings.