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

throttler

v0.0.1

Published

A little event-based util to make it easier to throttle execution for a batch of jobs.

Downloads

113

Readme

Throttler!

A little event-based util to make it easier to throttle execution for a batch of jobs.

Installation

npm install throttler

Usage

var Throttler = require('throttler');

var throttler = new Throttler({
	'maxActive' : 50,
	'wait' : 2500
});

for (var i = 0; i < 1000; i++) {
	throttler.add(function(callback) {
		// do some work
		callback(some, results);
	});
}

throttler.on('job-started', function(stats) {
	console.log('Job started. Current stats: %j', stats);
}).on('job-completed', function(stats) {
	console.log('Job completed. Current stats: %j', stats);
}).on('complete', function(stats) {
	console.log('All jobs complete. Final stats: %j', stats);
});

Details

When creating a new Throttler instance you can specify two options:

{
	'maxActive' : 50,	// The maximum number of jobs to run at once. Must be specified
	'wait' : 2500 		// The time, in milliseconds, to wait before trying to run more jobs if maxActive jobs are currently running
}

You then add jobs to the Throttler instance. Each job is simply a function that takes a single parameter: a callback to be invoked when their work is complete. As each job finishes doing its work, it should invoke this callback with any relevant arguments to notify the Throttler instance. As the Throttler works, it will fire off a variety of events that allow you to keep track of its progress, any arguments passed to the callback will be available via these events.

Events

Each event will be emitted with a stats object that gives you the current status of all jobs. For the two job-specific events (job-started and job-completed) the stats object will also have a reference to the job, including any arguments passed to the callback when the job completed.

job-started

{
	"active" : 2,					// The number of actively running jobs
	"pending" : 16,					// The number of pending jobs
	"complete" : 4,					// The number of completed jobs
	"averageRunTime" : 1756.5,		// The current average for how long each job takes to complete, in milliseconds
	"job" : {
		"identifier" : "Job #6",	// A system-generated job identifer
		"startTime" : 1363294870424	// The time the job was started
	}
}

job-completed

For the job-completed event, the stats object has all of the above properties, but the jobs property will contain additional properties

{
	"active" : 4,
	"pending" : 15,
	"complete" : 5,
	"averageRunTime" : 2002,
	"job" : {
		"identifier" : "Job #4",
		"startTime" : 1363294867922,
		"completedResults" : ["some","results"],	// The args passed to the callback when the job completed
		"elapsed" : 2984,							// The elapsed time to run the job, in milliseconds
		"complete" : true							// Did the job complete, will always be true here
	}
}

completed

For the final completed event, you'll simply get the non-job specific stats:

{
	"active" : 0,
	"pending" : 0,
	"complete" : 20,
	"averageRunTime" : 1678.9
}