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

@nicolawealth/rate_limit

v0.0.7

Published

<!-- markdownlint-disable-next-line MD013 --> ![Tests Passing](https://github.com/NicolaWealth/rate_limit/actions/workflows/test.yml/badge.svg) <!-- markdownlint-disable-next-line MD013 --> ![Code Cov](https://img.shields.io/badge/dynamic/json?url=https%3

Readme

rate_limit

Tests Passing

Code Cov

Lightweight utility for rate-limiting function calls in JavaScript/TypeScript. It helps control execution frequency and manage timing for synchronous and asynchronous operations.

Installation

Requires Node.js ≥ 18.

Install via npm: npm install @nicolawealth/rate_limit

Peer Dependency

This package relies on @nicolawealth/ioc as a peer dependency. It will not be bundled with rate_limit, so you need to install it in your project: npm install @nicolawealth/ioc

Compatibility Notes

The rate_limit package ships with two build formats:

ESM (Modern)

(dist/index.modern.js)

Recommended for modern bundlers like Webpack, Vite, or Rollup. Install both packages via npm & import:

import { rateLimitFactory } from '@nicolawealth/rate_limit';
import { ioc } from '@nicolawealth/ioc';

UMD

(dist/index.umd.js)

Suitable for script-tag usage in browsers or environments without module loaders. If you use the UMD build, you must ensure the peer dependency @nicolawealth/ioc is loaded first and exposed globally as window.ioc.

Example:

<script src="path/to/ioc.umd.js"></script>
<script src="path/to/rate_limit.umd.js"></script>

The UMD bundle will reference ioc as a global variable.

Why Use rate_limit?

Rate limiting is essential for:

  • Preventing API overload
  • Throttling expensive operations
  • Managing UI updates efficiently
  • Handling rapid event streams
  • Reducing redundant async calls

Interface

The package exports two functions:

  • rateLimitFactory(delayBetweenCallsMs, function()) and
  • rateLimitEmitLastFactory(delayBetweenCallsMs, asyncFunction(params), callback(result)):

Exports

RateLimitFactory

Creates a wrapper that ensures fn() runs at most once every delayBetweenCallsMs milliseconds.

Behavior (RateLimitFactory)

  • The first call executes immediately.
  • Subsequent calls within the delay window:
    • One deferred call is scheduled.
    • Additional calls are ignored until the scheduled call runs.

Example (RateLimitFactory)

import { rateLimitFactory } from '@nicolawealth/rate_limit';

const log = () => console.log('Action!');
const rateLimitedLog = rateLimitFactory(1000, log);

rateLimitedLog(); // Executes immediately
rateLimitedLog(); // Deferred
rateLimitedLog(); // Ignored

RateLimitEmitLastFactory

Wraps an async function so it runs at most once per delay window, but always processes the latest parameters.

Behavior (RateLimitEmitLastFactory)

  • The first call executes immediately.
  • Subsequent calls within the delay window:
    • Only the most recent parameters are retained.
    • Deferred execution uses the latest arguments.
  • callback(result) is invoked after each execution.

Example (RateLimitEmitLastFactory)

import { rateLimitEmitLastFactory } from '@nicolawealth/rate_limit';

const fetchData = async (query: string) => {
  // Simulate API call
  return `Result for ${query}`;
};

const handleResult = (data: string) => console.log(data);

const rateLimitedFetch = rateLimitEmitLastFactory(2000, fetchData, handleResult);

rateLimitedFetch('first');  // Executes immediately
rateLimitedFetch('second'); // Deferred, replaces previous
rateLimitedFetch('third');  // Deferred, replaces previous

Testing

Tests are located in src/rate_limit.test.ts and use:

  • Mocha for test runner
  • Sinon for mocking timers
  • NYC for coverage

Run tests:

npm run test

or for robust output:

npm run test-r

Run coverage:

npm run cover

For detailed HTML/LCOV output:

npm run cover:report

Development notes

  • npm run clean: Removes dist/ folder & caches
  • npm run build: Builds both formats
  • npm run lint: Runs ESLint
  • npm run doc: Generates documentation