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 🙏

© 2024 – Pkg Stats / Ryan Hefner

bunchie

v5.1.0

Published

A keyed debouncer.

Downloads

242

Readme

GitHub license npm version PRs Welcome

This is a utility to batching together many individual calls by timing or by count before proceeding as a batch.

Example

We want to pool multiple read requests to a cache together to take advantage of multi-get performance.

Say our code initially looks like this:

function readFromCache(key) {
  return cache.get(key);
}

Every time we call readFromCache, we do a single call to the cache. If we did 1000 requests within 200ms, it would do 1000 individual calls.

In order to let these bunch up for a multi-get, we can use bunchie.

import { bunch } from 'bunchie';


const readFromCache = bunch((keys) => {
  return cache.multiGet(keys);
}, {
  // This flag makes the `keys` argument above have *all* of the arguments
  // that have been passed to this functino as it has been being debounced
  // for the current batch. Default is simply the last.
  includeAllBatchArguments: true,
});

Now readFromCache will batch up requests before hitting the cache. We'll want to add some configuration.

Say that we want to wait 50 milliseconds for additional requests to the cache, otherwise we invoke the handler. We do this by adding a debounce property to the config.

const readFromCache = bunch((keys) => {
  return cache.multiGet(keys);
}, {
  debounce: 50,
  includeAllBatchArguments: true,
});

Now readFromCache can be invoked, and will wait up to 50ms for another request to come in before going to the cache. This is good, but we don't want to let this process be pushed back indefinitely, so let's say we'd like to have this go on for a maximum of 200ms. We do this through the maxTimeout property.

const readFromCache = bunch((keys) => {
  return cache.multiGet(keys);
}, {
  debounce: 50,
  maxTimeout: 200,
  includeAllBatchArguments: true,
});

And finally, let's say that there is a maximum amount of invocations we'd like to handle within a timeout before moving forward with the handle. Say we get 2000 requests in this 200ms period, and we only want to handle up to 100 keys in a single multiGet request. We do this through the maxCount property.

const readFromCache = bunch((keys) => {
  return cache.multiGet(keys);
}, {
  debounce: 50,
  maxTimeout: 200,
  maxCount: 100,
  includeAllBatchArguments: true,
});

So the result is, we could have these 3 requests come in as follows:

readFromCache('harry');
readFromCache('sally');
readFromCache('billy');

And given that they've all occured within 50ms of one another, the handler function will be invoked with a keys value of [['harry'], ['sally'], ['billy']], so we can do an efficient multiGet from the cache!

Advanced Example

In the example above, let's say that cache.multiGet takes an array of keys, and returns an array of responses in the same order. As we have it in that example, the response to each of the readFromCache requests will be the array of responses in that batch. Let's have it instead map the response to the corresponding key. Invoking a bunched function actually provides some useful information in the response object, namely an index property to indicate in what order this invocation was done in. We can use this to map the cache response to the key. The result property contains the result of the handler function.

const bunchedCache = bunch((keys) => {
  return cache.multiGet(keys);
}, {
  debounce: 50,
  maxTimeout: 200,
  maxCount: 100,
  includeAllBatchArguments: true,
  // This flag means we'll get additional metadata about the current batch
  // being invoked, as well as info about the specific invocation that we
  // just called. Some of this includes the `index`, seen here, which would
  // be the order number in which this invocation occurred for this batch.
  // Can be used to do something like what is demonstrated here, where the index
  // is used to get the appropriate item from the response object such that
  // it corresponds with this invocation.
  includeMetadataInResponse: true,
});
async function readFromCache(key) {
  const { index, result } = await bunchedCache(key);
  // `index` is the index this `key` is in the `multiGet`, and `result` is an array
  // of responses from the `multiGet`.
  return result[index];
}

So now we can do:

// Even though behind the scenes `readFromCache` bunched my requests together
// and got an array response, we now still get specifically the harryResult!
const harryResult = await readFromCache('harry');