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

throttling

v1.0.2

Published

Throttle a function and cache the result for x milliseconds

Downloads

24

Readme

throttling

Throttle a function and cache the result for x milliseconds.

Build Status

Example

var fs = require('fs');
var throttle = require('throttling');

var getFile = throttle(1000*60, function (callback) {
  fs.readFile('/etc/passwd', callback);
});

getFile(function (err, data) {
  console.log(data);
});

Usage

Requireing throttling returns a generator function. The most simple way to use throttling is to call this generator function with the function you want to throttle:

var calls = 0;
var runner = throttle(function () {
  return ++calls;
});

The generator function returns a runner function. The first time you call the runner function, the throttled function will be called and its output will be cached and returned by the runner function. By default subsequent calls to the runner function will not call the throttled function, but just return the cached result:

runner(); // => 1
runner(); // => 1
runner(); // => 1

Cache timeout

You can also set a cache timeout. Parse an integer representing the cache timeout in milliseconds as the first argument to the generator function:

var calls = 0;
var runner = throttle(1000, function () {
  return ++calls;
});

runner(); // => 1
runner(); // => 1
setTimeout(runner, 1001); // => 2

Async

The throttled function is called with a callback as the first argument. If your function needs to do any async work, call this with the result when done:

var runner = throttle(1000, function (callback) {
  process.nextTick(function () {
    callback(Math.random());
  });
});

To get the result of your async function, use a callback when calling the runner function:

runner(function (result) {
  console.log('The random number is:', result);
});

The arguments parsed to the callback will be cached according to the same rules as described previously, so subsequent executions of runner will just call the supplied callback function with the previous arguments until the cache expires.

Delayed callback

When setting up the runner function, it's possible to specify that the callback shouldn't be called with the cached arguments, but instead wait for the cache to expire, and then be called with the new callback arguments from the throttled function:

var calls = 0;
var options = {
  timeout: 1000,
  wait: true
};
var runner = throttle(options, function (callback) {
  process.nextTick(function () {
    callback(++calls);
  });
});

runner(function (result) {
  console.log('1st call:', result);
});
runner(function (result) {
  console.log('2nd call:', result);
});
runner(function (result) {
  console.log('3rd call:', result);
});

The above code will first output 1st call: 1. Then it will wait aproximently 1 second and output:

2nd call: 2
3rd call: 2

Notice how the timeout is now supplied using the timeout property in the options hash.

Error handling

If the first argument parsed to the fn callback is an Error, the callback will not be cached.

var calls = 0;
var runner = throttle(1000, function (callback) {
  callback(new Error(), ++calls);
});

var fn = function (err, result) {
  console.log(result);
};

runner(fn); // calls `fn` with the error and the result 1
runner(fn); // calls `fn` with the error and the result 2
runner(fn); // calls `fn` with the error and the result 3

Note that if options.wait is true, the throttling will still be in effect and the fn function will only be called once for every options.timeout.

License

MIT