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

transparent-herd

v2.0.0

Published

A package for grouping singular calls into bulk or batch calls transparently to the caller

Downloads

6

Readme

transparent-herd

What is this?

It's a package for grouping singular calls into bulk or batch calls, transparently to the caller

What does that mean?

- As you know most of times grouping your calls, for example you database writes, into a bulk one can greatly improve your performance, even of an order of magnitude.

- Yes

- Then, why don't you change your code to take advantage of that?

- It's complicated, all our calls are singular ones, it would require the entire rewrite of our service, including changing the way the clients call our API.

- Well, with this package you can change only your database access code, for example, and keep the rest of the application using singular calls.

- Cool! Show me an example.

Example

in this example the singular call to MongoDB's insertOne is converted to an insertMany in the batched function.

/*
* The batched function gets an array of function arguments and returns an array of promises
*/
const batched: transparentHerd.BatchedFunction = async (args) => {
  // the object to insert is the first argument of each list of arguments
  const documents = args.map((arg) => arg[0]);
  try {
    const result = await collection.insertMany(documents);
    return documents.map(() => Promise.resolve(result));
  } catch (e) {
    return documents.map(() => Promise.reject(e));
  }
};

/*
  * This way you get a singular function out of the batched one
  */
const singular: transparentHerd.SingularFunction = transparentHerd.singular(batched, { maxBatchSize });

/*
  * Then you can use the singular function just as before
  */
const allPromises = [];
for (let i = 0; i < numCalls; i++) {
  allPromises.push(singular({ a: i }));
}

The mean execution time out of 100 rounds was 15.29 times smaller with the convertion to bulk insert. See transparent-herd-test

Getting started

npm install --save transparent-herd

Documentation

Full documentation here

The singular function

Converts a batched functions to a singular one. Al least minConcurrent calls and at most maxConcurrent are done at a time, at most maxBatchSize is allocated to each call. When maxBatchSize is reached, more concurrent calls are made, until maxConcurrent is reached, if specified.

Params

batched: the batched function takes an array of arguments and returns an array of promises

minConcurrent the minimum number of concurrent calls or 1 if not specified

maxConcurrent the maximum number of concurrent calls, only if maxBatchSize is passed

maxBatchSize the maximum batch size allocated to a call

Returns

the singular function, taking different arguments and returning a promise