Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including batchify with all npm packages installed. Try it out:

var batchify = require("batchify")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

batchify v0.1.0

Build Status

Batchify

In some situations, many identical operations can occur in a short period of time, causing performance issues.

Batchify wraps a function so that when it is invoked with identical arguments multiple times while in progress, it will only be invoked once, and all callbacks will be notified when it completes.

  users.findAll(function(err, users) {
    # do something with each user
  });

If there are many concurrent requests that need to look up all of the users, each one will take time and resources to service.

With batchify, one request will be used to service all of the concurrent requests:

  var batchedFindAll = Batchify.wrap(users, 'findAll');

  # Now you use batchedFindAll the same as you would the original function:
  batchedFindAll(function(err, users) { ... });

Batchify invokes users.findAll once and triggers all of the subscribing callbacks when it is finished.

The wrapped function can have parameters too.

(Batchify uses the parameter values as a hash key, so concurrent calls with different parameters will not conflict)

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free