limiter vs p-limit vs p-throttle
Concurrency Control Libraries
limiterp-limitp-throttleSimilar Packages:

Concurrency Control Libraries

Concurrency control libraries are essential tools in web development that help manage the execution of asynchronous operations, ensuring that they do not overwhelm system resources or violate rate limits. These libraries provide mechanisms to limit the number of concurrent operations, throttle requests over time, or impose specific execution constraints. By using these libraries, developers can optimize performance, enhance user experience, and prevent server overloads, especially when dealing with APIs or resource-intensive tasks.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
limiter11,188,3161,558158 kB14a year agoMIT
p-limit02,80714.9 kB0a month agoMIT
p-throttle051321.6 kB04 months agoMIT

Feature Comparison: limiter vs p-limit vs p-throttle

Concurrency Management

  • limiter:

    Limiter provides a straightforward API to set limits on how many times a function can be executed within a defined time frame. It is particularly useful for controlling the rate of function calls, making it suitable for scenarios like API requests where rate limits are enforced.

  • p-limit:

    p-limit allows you to control the number of concurrent promises that can be executed at any given time. This is crucial for managing resource-intensive tasks, as it helps prevent overwhelming the system by limiting how many promises are processed simultaneously.

  • p-throttle:

    p-throttle provides a mechanism to throttle function calls, ensuring that a function is only executed once within a specified time interval. This is particularly useful for scenarios like debouncing user input or limiting the frequency of API calls.

Use Cases

  • limiter:

    Limiter is ideal for scenarios where you need to enforce strict rate limits on function calls, such as when interacting with APIs that have usage quotas or when you want to limit the frequency of certain operations to prevent resource exhaustion.

  • p-limit:

    p-limit is best suited for scenarios where you need to perform multiple asynchronous operations, such as fetching data from multiple sources or processing files, while ensuring that only a certain number of these operations are executed concurrently to maintain performance.

  • p-throttle:

    p-throttle is particularly effective in scenarios where you want to limit the frequency of event handling, such as in user interface interactions (e.g., scrolling, resizing) or when making frequent API calls that should not exceed a certain rate.

Performance Optimization

  • limiter:

    By limiting the number of function calls, Limiter helps optimize performance by preventing excessive load on the system, ensuring that resources are used efficiently and that the application remains responsive even under heavy usage.

  • p-limit:

    p-limit enhances performance by allowing developers to control the concurrency of promise executions, which can lead to better resource management and improved response times when dealing with multiple asynchronous tasks.

  • p-throttle:

    p-throttle optimizes performance by ensuring that functions are not called more frequently than necessary, which can help reduce the load on servers and improve the overall efficiency of the application.

Simplicity and Ease of Use

  • limiter:

    Limiter offers a simple and intuitive API, making it easy to implement rate limiting in your applications without requiring extensive configuration or complex setups.

  • p-limit:

    p-limit is designed to be straightforward to use, allowing developers to quickly set up concurrency limits for their asynchronous operations with minimal overhead.

  • p-throttle:

    p-throttle provides a clean and easy-to-understand interface for throttling function calls, making it accessible for developers looking to implement rate limiting in their applications.

Flexibility

  • limiter:

    Limiter is flexible in that it allows developers to define custom limits based on their specific needs, making it adaptable to various use cases and scenarios.

  • p-limit:

    p-limit offers flexibility in managing the concurrency of promises, allowing developers to adjust the maximum number of concurrent executions based on system capabilities and requirements.

  • p-throttle:

    p-throttle is flexible in its configuration, enabling developers to easily specify the time interval for throttling, making it suitable for a wide range of applications.

How to Choose: limiter vs p-limit vs p-throttle

  • limiter:

    Choose Limiter if you need a simple and effective way to control the rate of function executions, especially in scenarios where you want to limit the number of times a function can be called over a specified period. It is particularly useful for scenarios like API rate limiting.

  • p-limit:

    Select p-limit when you want to manage the concurrency of promises, allowing you to specify the maximum number of promises that can be executed simultaneously. This is ideal for scenarios where you need to perform multiple asynchronous operations but want to avoid overwhelming the system with too many concurrent requests.

  • p-throttle:

    Opt for p-throttle if you need to limit the execution rate of a function over time, ensuring that it is called at most once in a specified duration. This is particularly useful for scenarios like handling user input events or API calls that should not be made too frequently.

README for limiter

limiter

Build Status NPM Downloads

Provides a generic rate limiter for the web and node.js. Useful for API clients, web crawling, or other tasks that need to be throttled. Two classes are exposed, RateLimiter and TokenBucket. TokenBucket provides a lower level interface to rate limiting with a configurable burst rate and drip rate. RateLimiter sits on top of the token bucket and adds a restriction on the maximum number of tokens that can be removed each interval to comply with common API restrictions such as "150 requests per hour maximum".

Installation

yarn add limiter

Usage

A simple example allowing 150 requests per hour:

import { RateLimiter } from "limiter";

// Allow 150 requests per hour (the Twitter search limit). Also understands
// 'second', 'minute', 'day', or a number of milliseconds
const limiter = new RateLimiter({ tokensPerInterval: 150, interval: "hour" });

async function sendRequest() {
  // This call will throw if we request more than the maximum number of requests
  // that were set in the constructor
  // remainingRequests tells us how many additional requests could be sent
  // right this moment
  const remainingRequests = await limiter.removeTokens(1);
  callMyRequestSendingFunction(...);
}

Another example allowing one message to be sent every 250ms:

import { RateLimiter } from "limiter";

const limiter = new RateLimiter({ tokensPerInterval: 1, interval: 250 });

async function sendMessage() {
  const remainingMessages = await limiter.removeTokens(1);
  callMyMessageSendingFunction(...);
}

The default behaviour is to wait for the duration of the rate limiting that's currently in effect before the promise is resolved, but if you pass in "fireImmediately": true, the promise will be resolved immediately with remainingRequests set to -1:

import { RateLimiter } from "limiter";

const limiter = new RateLimiter({
  tokensPerInterval: 150,
  interval: "hour",
  fireImmediately: true
});

async function requestHandler(request, response) {
  // Immediately send 429 header to client when rate limiting is in effect
  const remainingRequests = await limiter.removeTokens(1);
  if (remainingRequests < 0) {
    response.writeHead(429, {'Content-Type': 'text/plain;charset=UTF-8'});
    response.end('429 Too Many Requests - your IP is being rate limited');
  } else {
    callMyMessageSendingFunction(...);
  }
}

A synchronous method, tryRemoveTokens(), is available in both RateLimiter and TokenBucket. This will return immediately with a boolean value indicating if the token removal was successful.

import { RateLimiter } from "limiter";

const limiter = new RateLimiter({ tokensPerInterval: 10, interval: "second" });

if (limiter.tryRemoveTokens(5))
  console.log('Tokens removed');
else
  console.log('No tokens removed');

To get the number of remaining tokens outside the removeTokens promise, simply use the getTokensRemaining method.

import { RateLimiter } from "limiter";

const limiter = new RateLimiter({ tokensPerInterval: 1, interval: 250 });

// Prints 1 since we did not remove a token and our number of tokens per
// interval is 1
console.log(limiter.getTokensRemaining());

Using the token bucket directly to throttle at the byte level:

import { TokenBucket } from "limiter";

const BURST_RATE = 1024 * 1024 * 150; // 150KB/sec burst rate
const FILL_RATE = 1024 * 1024 * 50; // 50KB/sec sustained rate

// We could also pass a parent token bucket in to create a hierarchical token
// bucket
// bucketSize, tokensPerInterval, interval
const bucket = new TokenBucket({
  bucketSize: BURST_RATE,
  tokensPerInterval: FILL_RATE,
  interval: "second"
});

async function handleData(myData) {
  await bucket.removeTokens(myData.byteLength);
  sendMyData(myData);
}

Additional Notes

Both the token bucket and rate limiter should be used with a message queue or some way of preventing multiple simultaneous calls to removeTokens(). Otherwise, earlier messages may get held up for long periods of time if more recent messages are continually draining the token bucket. This can lead to out of order messages or the appearance of "lost" messages under heavy load.

License

MIT License