npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

bloom-filter-numbers

v1.0.0

Published

Lightweight Bloom Filter for integers using splitmix64 and XOR fingerprinting

Readme

bloom-filter-numbers

A compact and efficient Bloom Filter implementation optimized for numbers, written in pure JavaScript.
It uses the high-quality splitmix64 as the hashing function and XOR-based fingerprinting to track inserted values.


🚀 Features

  • ⚡ Fast and lightweight
  • 🔢 Optimized for integer keys (numbers / BigInts)
  • 🧠 Tracks inserted values via a fingerprint
  • 📤 Exportable & importable state
  • 📦 No dependencies, works everywhere

✅ When to Use

This library is ideal for optimizing workflows where quick existence checks on numeric values are needed. Typical use cases include:

  • Deduplication of numeric IDs such as user IDs, transaction hashes, or log entries
  • Real-time data filtering to prevent reprocessing of already-seen events
  • P2P systems and messaging layers to track seen message IDs, peers, or blocks
  • Efficient sync algorithms, where Bloom filters can be exchanged to quickly detect missing items before deeper comparison
  • Fingerprint verification: compare filters without transferring all data
  • Performance-critical environments, where string hashing is too heavy or unnecessary

🔢 This implementation is optimized specifically for numbers (integers or BigInts), making it faster and more memory-efficient than generic string-based Bloom filters. It avoids encoding overhead and suits low-level protocols and systems that operate on numeric identifiers.


🧠 Why use a Bloom Filter?

Bloom filters are probabilistic data structures that can quickly tell if a value is definitely not present — or maybe present.

  • ✅ If has(x) returns false, you can be 100% sure the value does not exist.
  • ❓ If has(x) returns true, the value might exist — and you can then run a deeper check (like querying a database).

This makes Bloom filters perfect for reducing unnecessary reads from databases or other expensive operations.

💡 Example:

if (!bloom.has(userId)) {
  // Definitely not seen before — safe to skip DB
  handleNewUser(userId);
} else {
  // Might have seen — verify with the database
  db.checkUser(userId).then(user => {
    if (!user) handleNewUser(userId);
  });
}

📦 Installation

npm install bloom-filter-numbers

📚 Usage

const BloomFilterNumbers = require('bloom-filter-numbers');

// Create a Bloom Filter with 1024 bits and 4 hash functions
const bloom = new BloomFilterNumbers(1024, 4);

// Add values
bloom.add(123);
bloom.add(456);

// Check existence
console.log(bloom.has(123)); // true
console.log(bloom.has(999)); // false

// Get internal stats
console.log(bloom.getStats());

♻️ Export & Import

You can save the filter state and restore it later (for caching, syncing, persistence, etc).

🔄 Export:

const exported = bloom.export();

/*
{
  bits: Uint8Array(...),
  size: 1024,
  numHashes: 4,
  itemCount: 2,
  totalFingerprint: 812374123
}
*/

🔁 Import:

const restored = new BloomFilterNumbers(
  exported.bits,
  exported.numHashes,
  exported.itemCount
);

console.log(restored.has(123)); // true

✅ You must provide all three: bits, numHashes, and itemCount when importing.


🧠 Fingerprinting

Every inserted number is XORed into an internal fingerprint:

console.log(bloom.export().totalFingerprint); // Unique to inserted values

📊 Stats Example

bloom.getStats();
/*
{
  sizeBits: 1024,
  numHashes: 4,
  insertedItems: 2,
  filterSizeBytes: 128,
  rawSizeBytes: 16,
  savedBytes: -112,
  savedPercent: -700,
  fingerprintSafeNumber: 812374123
}
*/

🛠️ Configuration Tips

  • Use size = itemCount * 10 for ~1% false positive rate
  • Increase numHashes (default: 4) for slightly better precision
  • Store bits, numHashes, itemCount if you want to restore the filter later

🔗 Project Links


🧑‍💻 Author

Created with ❤️ by colocohen


📝 License

MIT License


🤝 Contribute or Support

If you find this useful, feel free to star ⭐ the repo or sponsor me.

Pull requests are welcome!