https://github.com/ceejbot/farmfilter
https://github.com/ceejbot/farmfilter
Last synced: 16 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/ceejbot/farmfilter
- Owner: ceejbot
- License: other
- Created: 2017-10-19T18:20:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-10T19:17:31.000Z (about 8 years ago)
- Last Synced: 2026-05-30T18:28:13.351Z (about 1 month ago)
- Language: JavaScript
- Size: 92.8 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Yet another Bloom filter implementation for node.js. Everybody has to write one, as you know. Backed by FarmHash.
To install: `npm install farmfilter`
[](https://www.npmjs.com/package/farmfilter) [](https://travis-ci.org/ceejbot/farmfilter) [](https://coveralls.io/github/ceejbot/farmfilter?branch=master)
## Usage
To create a filter, pass an options hash to the constructor:
```javascript
const Filter = require('farmfilter');
const options =
{
bits: 1024,
hashes: 7,
seeds: [1, 2, 3, 4, 5, 6, 7]
};
const filter = new BloomFilter(options);
```
You can pass in seeds for the hash functions if you like, or they'll be randomly generated. Seeds must be integers.
You may also pass in a buffer as generated by `filter.toBuffer()`.
### createOptimal()
To create a filter optimized for the number of items you'll be storing and a desired error rate:
`filter = Filter.createOptimal(estimatedItemCount, errorRate);`
The error rate parameter is optional. It defaults to 0.005, or a 0.5% rate. This is probably the constructor you want to use.
### add()
`filter.add('cat');`
Adds the given item to the filter. Can also accept buffers and arrays containing strings or buffers:
`filter.add(['cat', 'dog', 'coati', 'red panda']);`
### has()
To test for membership:
`filter.has('dog');`
### clear()
To clear the filter:
`filter.clear();`
### toBuffer()
Returns a buffer with seeds and filter data.
### fromBuffer()
Reconstitutes a filter from a freeze-dried buffer.
## Licence
ISC.