Skip to content

Instantly share code, notes, and snippets.

@HerbCaudill
Created November 18, 2020 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HerbCaudill/bfc7dd57d5e8f1c20a49e2ae65135a3b to your computer and use it in GitHub Desktop.
Save HerbCaudill/bfc7dd57d5e8f1c20a49e2ae65135a3b to your computer and use it in GitHub Desktop.
Self-optimizing Bloom filter
import { BloomFilter } from 'bloomfilter' // https://github.com/jasondavies/bloomfilter.js
/**
* Returns an optimally configured Bloom filter for the given number of elements, using the
* calculations from https://www.di-mgt.com.au/bloom-filter.html .
* @param n The number of elements to be added to the Bloom filter
* @param p The target false positive rate
*/
const optimalBloomFilter = (n: number, p: number = 0.01) => {
const LOG2 = Math.log(2)
const m = Math.ceil((n * Math.log(p)) / Math.log(1 / 2 ** LOG2))
const m2 = nextPowerOf2(m)
const k = Math.ceil((LOG2 * m) / n)
console.log({ n, m, m_round: m2, k, p })
return new BloomFilter(m2, k)
}
const nextPowerOf2 = (m: number) => 2 ** Math.ceil(Math.log2(m - 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment