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 🙏

© 2026 – Pkg Stats / Ryan Hefner

throttle-memo

v1.2.0

Published

A memoization works with async tasks that can be throttled.

Readme

throttle-memo

A memoization works with async tasks that can be throttled.

Example

For more, see test/.

// consider a case of web server
// in client(many of them run concurrently):
request(server, args, (err, result) {
    // get response
});

// in server:
const tMemoWrapper = require('.test/wrapper');

function asyncTask(args, callback) {
    // some async task
}
// the signature of asyncTaskTMemoized is identical with asyncTask
let asyncTaskTMemoized = tMemoWrapper.tMemoize(asyncTask);

listen((request, response) => {
    asyncTaskTMemoized(request.args, (err, result) => {
        // only the first asyncTask will actually execute with a batch of requests
        // if no err, the result of asyncTask will be cached for any later requests
        // with same `args`
        response.send(err, result);
    });
});

Description

It will only call the first one of a batch of async tasks that come in together, and when it has result, share it to all other tasks. If no error occurred, it will cache the result for any later tasks by default. So it is a kind of throttle from the aspect of the dependencies of those async tasks, and a memoization from the aspect of those tasks.

A possible scene is that requests come in concurrently, and when the first of those requests execute, some setup should be done and should only be done once, after that, the result can be share between all requests. (You might consider to use some kind of lock or queue to handle the concurrency, and cache the result to optimize performance in this case. FYI an implementation from reddit.) For example, lots of people ask a room for meeting from time to time, and when the first person comes, a meeting room should be prepared, then the room number should be given to this person and all others will attend the same meeting.

Also notice that this package is not particular designed for distributed system.

Actually, I am not sure what this programming idiom composition really is or if it already has an mature implemention. Feedback is welcome.

Installation

$ npm install throttle-memo

API

new ThrottleMemo(tag[, options])

Init an instance with tag as unique identifier and options.

The disableCache property of options is a bool value to control if the result will be kept after the wrapped function executed. Default is false, so next run of the same instance will directly return the result it cached.

throttleMemo.push(func, ...args)

Push a function and its arguments, expect for callback when a result in the same throttle-memo is came back.

The func is expect to be a async task, and the last of args should be its callback, with signature callback(err, result).

Return true if push success, else return false.

size

A getter of the length of tasks in throttle-memo.

Tests

$ npm test/test # local test
# or for network test:
$ node test/server
$ node test/client  # in other cli

Changelog

  • 1.2.0: add options to enable/disable cache
  • 1.1.0: add cache for result by default

License

MIT