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

async-limiter

v2.0.0

Published

asynchronous function queue with adjustable concurrency

Downloads

38,371,850

Readme

Async-Limiter

A module for limiting concurrent asynchronous actions in flight. Forked from queue.

npm tests coverage

This module exports a class Limiter that implements some of the Array API. Pass async functions (ones that accept a callback or return a promise) to an instance's additive array methods.

Motivation

Certain functions, like zlib, have undesirable behavior when run at infinite concurrency.

In this case, it is actually faster, and takes far less memory, to limit concurrency.

This module should do the absolute minimum work necessary to queue up functions. PRs are welcome that would make this module faster or lighter, but new functionality is not desired.

Style should confirm to nodejs/node style.

Example

var Limiter = require('async-limiter');

var t = new Limiter({ concurrency: 2 });
var results = [];

// add jobs using the familiar Array API
t.push(function(cb) {
  results.push('two');
  cb();
});

t.push(
  function(cb) {
    results.push('four');
    cb();
  },
  function(cb) {
    results.push('five');
    cb();
  }
);

t.unshift(function(cb) {
  results.push('one');
  cb();
});

t.splice(2, 0, function(cb) {
  results.push('three');
  cb();
});

// Jobs run automatically on the next tick.
// If you want a callback when all are done, call 'onDone()'.
t.onDone(function() {
  console.log('all done:', results);
});

Zlib Example

const zlib = require('zlib');
const Limiter = require('async-limiter');

const message = { some: 'data' };
const payload = new Buffer(JSON.stringify(message));

// Try with different concurrency values to see how this actually
// slows significantly with higher concurrency!
//
// 5:        1398.607ms
// 10:       1375.668ms
// Infinity: 4423.300ms
//
const t = new Limiter({ concurrency: 5 });
function deflate(payload, cb) {
  t.push(function(done) {
    zlib.deflate(payload, function(err, buffer) {
      done();
      cb(err, buffer);
    });
  });
}

console.time('deflate');
for (let i = 0; i < 30000; ++i) {
  deflate(payload, function(err, buffer) {});
}
t.onDone(function() {
  console.timeEnd('deflate');
});

Install

npm install async-limiter

Test

npm test

API

var t = new Limiter([opts])

Constructor. opts may contain inital values for:

  • t.concurrency

Instance methods

t.onDone(fn)

fn will be called once and only once, when the queue is empty. If the queue is empty on the next tick, onDone() will be called.

Instance methods mixed in from Array

Mozilla has docs on how these methods work here.

t.push(element1, ..., elementN)

t.unshift(element1, ..., elementN)

t.splice(index , howMany[, element1[, ...[, elementN]]])

On the next tick, job processing will start.

Properties

t.concurrency

Max number of jobs the queue should process concurrently, defaults to Infinity.

t.length

Jobs pending + jobs to process (readonly).