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 🙏

© 2026 – Pkg Stats / Ryan Hefner

adaptive-bloom-filter

v1.1.0

Published

Fast, memory-efficient bloom filter with adaptive compression and async operations for JavaScript

Readme

Fast Bloom Filter for JavaScript

npm version License: MIT GitHub stars GitHub issues

Stop wasting memory on massive lookup tables. This bloom filter tells you instantly if something is definitely not there or might be there. Perfect for web apps, databases, and real-time systems.

Why Use This?

Save 90% memory compared to storing full datasets. Check millions of items in microseconds.

Real companies use bloom filters for:

  • Google: Web crawling and Chrome ad blocking
  • Netflix: Recommendation systems
  • Cloudflare: DDoS protection
  • Bitcoin: Transaction validation

Quick Start

# Install via npm
npm install adaptive-bloom-filter

# Or download directly
curl -O https://raw.githubusercontent.com/leonkalema/bloom/main/src/index.js

# Or use CDN
<script src="https://cdn.jsdelivr.net/gh/leonkalema/bloom@main/src/index.js"></script>
// Create filter for 10,000 items with 1% false positive rate
const filter = new AdaptiveBloomFilter(10000, 0.01);

// Add items
filter.add("user-123");
filter.add("session-abc");

// Check items (instant response)
filter.check("user-123");    // true - might be there
filter.check("user-999");    // false - definitely not there

Real-World Examples

Block Malicious IPs

const ipFilter = new AdaptiveBloomFilter(1000000, 0.001);

// Add known bad IPs
badIPs.forEach(ip => ipFilter.add(ip));

// Check incoming requests (microsecond response)
if (ipFilter.check(requestIP)) {
  // Might be malicious - check database
  const isBad = await database.checkIP(requestIP);
} else {
  // Definitely safe - skip database check
  allowRequest();
}

Avoid Duplicate Processing

const processedFilter = new AdaptiveBloomFilter(50000, 0.01);

async function processFile(filename) {
  if (processedFilter.check(filename)) {
    return; // Already processed or check database
  }
  
  // Definitely not processed - safe to proceed
  await doExpensiveProcessing(filename);
  processedFilter.add(filename);
}

Cache Hit Prediction

const cacheFilter = new AdaptiveBloomFilter(100000, 0.05);

// Track cached items
cache.on('set', (key) => cacheFilter.add(key));

// Fast cache check
function getData(key) {
  if (!cacheFilter.check(key)) {
    // Definitely not cached - skip cache lookup
    return database.get(key);
  }
  
  // Might be cached - check cache first
  return cache.get(key) || database.get(key);
}

Advanced Features

Memory Compression

Automatically compresses when beneficial. Saves 20-80% memory on sparse data.

const filter = new AdaptiveBloomFilter(1000000, 0.01);

// Add 100k items
for (let i = 0; i < 100000; i++) {
  filter.add(`item-${i}`);
}

// Check compression stats
const stats = filter.getMetrics();
console.log(`Memory saved: ${stats.estimatedMemorySavings} bytes`);

Async Operations

Prevent UI blocking during heavy operations.

// Add 10k items without blocking
const promises = [];
for (let i = 0; i < 10000; i++) {
  promises.push(filter.addAsync(`item-${i}`));
}
await Promise.all(promises);

// Check items asynchronously
const exists = await filter.checkAsync("item-5000");

Performance Monitoring

Track real performance metrics.

const metrics = filter.getMetrics();
console.log({
  operations: metrics.addOperations + metrics.checkOperations,
  avgSpeed: metrics.averageCheckTime,
  memoryUsage: metrics.estimatedMemoryUsage,
  falsePositiveRate: metrics.currentFalsePositiveRate
});

Configuration

new AdaptiveBloomFilter(expectedItems, falsePositiveRate)

expectedItems: How many items you plan to add falsePositiveRate: Acceptable false positive rate (0.01 = 1%)

Choosing Parameters

| Use Case | Items | False Positive Rate | Memory Usage | |----------|-------|-------------------|--------------| | IP blocking | 1M | 0.001 (0.1%) | ~1.8 MB | | Cache checking | 100K | 0.05 (5%) | ~95 KB | | Duplicate detection | 10K | 0.01 (1%) | ~12 KB |

Browser Support

Works in all modern browsers and Node.js. Zero dependencies.

  • Chrome 45+
  • Firefox 40+
  • Safari 10+
  • Edge 12+
  • Node.js 6+

Performance

Speed: 1-5 microseconds per operation Memory: 10-15 bits per item (vs 32+ bytes for hash tables) Accuracy: Configurable false positive rate, zero false negatives

Tested with:

  • ✅ 10 million items
  • ✅ 1000 operations per second
  • ✅ Mobile devices
  • ✅ Web workers

API Reference

Constructor

new AdaptiveBloomFilter(expectedItems, falsePositiveRate)

Methods

filter.add(item)           // Add item to filter
filter.check(item)         // Check if item exists
filter.addAsync(item)      // Add item asynchronously  
filter.checkAsync(item)    // Check item asynchronously
filter.compress()          // Force compression
filter.reset()             // Clear all data
filter.getMetrics()        // Get performance stats

Common Questions

Q: What happens if I add more items than expected? A: False positive rate increases. The filter still works but becomes less accurate.

Q: Can I remove items? A: No. Bloom filters only support add and check operations. Use a counting bloom filter for removals.

Q: How accurate is it? A: Zero false negatives. False positives match your configured rate (1% default).

Q: Does it work with objects? A: Only strings. Convert objects to JSON strings first.

License

MIT License. Use anywhere, including commercial projects.

Contributing

Found a bug? Want a feature? Open an issue or submit a pull request.

Performance improvements welcome - this filter is used in production systems.