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

mergebounce

v0.1.1

Published

A debounce implementation that merges passed in objects before finally sending them to the debounced function

Downloads

1,279

Readme

mergebounce

mergebounce is a fork of lodash's debounce function with the added feature that it'll use the lodash merge function to merge all passed in parameters before eventually invoking the debounced inner function.

By doing so, it's possible to combine multiple expensive calls into a single one without losing state changes that would have been made by individual calls.

usecases

Batching expensive writes

Imagine you have a frontend app that stores data in IndexedDB via localForage. Writes to IndexedDB are slow, which can slow down your whole app.

You can increase performance by batching writes together.

One way to do this, is to install localForage-setItems and then to use mergebounce to combine multple calls to setItems into a single one.

const debouncedSetItems = mergebounce(
    items => store.setItems(items),
    50,
    {'promise': true}
);

function save (json) {
    const items = {}
    items[json.id)] = json;
    return debouncedSetItems(items);
}

Now save can be called many times, but the actual writes (calls to setItems) will be far fewer.

Reducing requests

Let's suppose you have a chat app and as chat messages appear you want to fetch user data for new message authors.

When you initially load the chat, there might be a 100 messages with 50 authors.

Instead of making a request for every incoming message that has an as yet unknown author, you can mergebounce the function and combine multiple calls into a single request.

For example:

async function _fetchUserData(nicknames) {
  const response = await fetch('/user/data', {
    body: JSON.stringify({nicknames}),
    headers: { 'Content-Type': 'application/json' },
  });

  data.forEach(userdata => getUser().update(userdata));
}

const fetchUserData = mergebounce(_fetchUserData, 250, {'concatArrays': true});

// The following calls with result in one request with all the nicnames
// concatenated into one array
fetchUserData(['coolguy69', 'ilikecats', 'dielan']);
fetchUserData(['coolboymew']);
fetchUserData(['donkeykong']);

options

The default debounce options are allowed, as well as the following option:

  • concatArrays: By default arrays will be treated as objects when being merged. When merging two arrays, the values in the 2nd arrray will replace the corresponding values (i.e. those with the same indexes) in the first array. When concatArrays is set to true, arrays will be concatenated instead.
  • dedupeArrays: This option is similar to concatArrays, except that the concatenated array will also be deduplicated. Thus any entries that are concatenated to the existing array, which are already contained in the existing array, will first be removed.
  • promise: By default, when calling a mergebounced function that doesn't execute immediately, you'll receive the result from its previous execution, or undefined if it has never executed before. By setting the promise option to true, a promise will be returned instead of the previous execution result when the function is debounced. The promise will resolve with the result of the next execution, as soon as it happens.