p-limit vs limiter vs p-throttle
JavaScriptの制限ライブラリ
p-limitlimiterp-throttle類似パッケージ:
JavaScriptの制限ライブラリ

これらのライブラリは、非同期処理の実行を制限するためのツールです。特に、APIリクエストやデータベースクエリなどのリソースを消費する操作において、過剰な負荷を防ぐために使用されます。これにより、システムの安定性を保ちながら、効率的に処理を行うことができます。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
p-limit181,207,0172,69111.7 kB52ヶ月前MIT
limiter9,509,5271,556158 kB1410ヶ月前MIT
p-throttle2,559,14150821.6 kB01ヶ月前MIT
機能比較: p-limit vs limiter vs p-throttle

基本機能

  • p-limit:

    p-limitは、Promiseを使用した非同期処理の同時実行数を制御します。これにより、リソースの消費を抑えつつ、効率的に処理を行うことができます。

  • limiter:

    Limiterは、特定の時間内に実行できる操作の数を制限します。これにより、サーバーやAPIへの過剰なリクエストを防ぎ、システムの安定性を向上させます。

  • p-throttle:

    p-throttleは、特定の時間間隔で関数を呼び出すことを制限します。これにより、頻繁なイベントの発生を抑制し、パフォーマンスを向上させます。

使用シナリオ

  • p-limit:

    データベースクエリやファイルの読み込みなど、複数の非同期処理を同時に実行する必要がある場合に便利です。

  • limiter:

    APIリクエストの制限や、外部サービスへの接続制御に適しています。特に、サーバーが過負荷になるのを防ぐために使用されます。

  • p-throttle:

    ユーザーのスクロールやクリックイベントなど、頻繁に発生するイベントに対して、処理を制限したい場合に使用されます。

設計原則

  • p-limit:

    p-limitは、Promiseの実行を管理するために設計されており、非同期処理の制御を簡素化します。直感的なAPIを提供し、使いやすさを重視しています。

  • limiter:

    Limiterは、トークンバケットアルゴリズムを使用して、リクエストの制限を行います。このアプローチは、柔軟性があり、さまざまなシナリオに適応可能です。

  • p-throttle:

    p-throttleは、時間ベースの制限を実装しており、特定の時間間隔での関数呼び出しを保証します。これにより、リソースの無駄遣いを防ぎます。

パフォーマンス

  • p-limit:

    p-limitは、同時実行数を制御することで、リソースの使用を最適化し、パフォーマンスを向上させます。これにより、システムが過負荷になるのを防ぎます。

  • limiter:

    Limiterは、リクエスト数を制限することで、サーバーの負荷を軽減し、全体的なパフォーマンスを向上させます。過剰なリクエストを防ぐため、安定した動作を実現します。

  • p-throttle:

    p-throttleは、関数の呼び出しを制限することで、特に高頻度のイベントにおいてパフォーマンスを最適化します。これにより、無駄な処理を減らし、効率的な動作を実現します。

学習曲線

  • p-limit:

    p-limitも直感的なAPIを持っており、Promiseを使用した非同期処理に慣れている開発者にとっては学習が容易です。

  • limiter:

    Limiterは、シンプルなAPIを提供しており、比較的簡単に学ぶことができます。基本的な概念を理解すれば、すぐに使用を開始できます。

  • p-throttle:

    p-throttleは、時間間隔での制限を理解するだけで良いため、学習曲線は比較的緩やかです。基本的な使用法をすぐに習得できます。

選び方: p-limit vs limiter vs p-throttle
  • p-limit:

    p-limitは、Promiseの同時実行数を制御したい場合に適しています。非同期処理を効率的に管理したい場合に選択してください。

  • limiter:

    Limiterは、特定の時間枠内でのリクエスト数を制限したい場合に最適です。特に、トラフィックが多い環境での使用が推奨されます。

  • p-throttle:

    p-throttleは、特定の時間間隔での関数呼び出しを制限したい場合に最適です。例えば、ユーザーの入力に応じてイベントをトリガーする場合に便利です。

p-limit のREADME

p-limit

Run multiple promise-returning & async functions with limited concurrency

Works in Node.js and browsers.

Install

npm install p-limit

Usage

import pLimit from 'p-limit';

const limit = pLimit(1);

const input = [
	limit(() => fetchSomething('foo')),
	limit(() => fetchSomething('bar')),
	limit(() => doSomething())
];

// Only one promise is run at once
const result = await Promise.all(input);
console.log(result);

API

pLimit(concurrency) default export

Returns a limit function.

concurrency

Type: number
Minimum: 1

Concurrency limit.

limit(fn, ...args)

Returns the promise returned by calling fn(...args).

fn

Type: Function

Promise-returning/async function.

args

Any arguments to pass through to fn.

Support for passing arguments on to the fn is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions.

limit.map(iterable, mapperFunction)

Process an iterable of inputs with limited concurrency.

The mapper function receives the item value and its index.

Returns a promise equivalent to Promise.all(Array.from(iterable, (item, index) => limit(mapperFunction, item, index))).

This is a convenience function for processing inputs that arrive in batches. For more complex use cases, see p-map.

limit.activeCount

The number of promises that are currently running.

limit.pendingCount

The number of promises that are waiting to run (i.e. their internal fn was not called yet).

limit.clearQueue()

Discard pending promises that are waiting to run.

This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.

Note: This does not cancel promises that are already running.

limit.concurrency

Get or set the concurrency limit.

limitFunction(fn, options) named export

Returns a function with limited concurrency.

The returned function manages its own concurrent executions, allowing you to call it multiple times without exceeding the specified concurrency limit.

Ideal for scenarios where you need to control the number of simultaneous executions of a single function, rather than managing concurrency across multiple functions.

import {limitFunction} from 'p-limit';

const limitedFunction = limitFunction(async () => {
	return doSomething();
}, {concurrency: 1});

const input = Array.from({length: 10}, limitedFunction);

// Only one promise is run at once.
await Promise.all(input);

fn

Type: Function

Promise-returning/async function.

options

Type: object

concurrency

Type: number
Minimum: 1

Concurrency limit.

FAQ

How is this different from the p-queue package?

This package is only about limiting the number of concurrent executions, while p-queue is a fully featured queue implementation with lots of different options, introspection, and ability to pause the queue.

Related

  • p-throttle - Throttle promise-returning & async functions
  • p-debounce - Debounce promise-returning & async functions
  • p-map - Run promise-returning & async functions concurrently with different inputs
  • p-all - Run promise-returning & async functions concurrently with optional limited concurrency
  • More…