p-limit vs async vs bottleneck vs rate-limiter-flexible vs promise-limit
Node.jsの非同期処理ライブラリ
p-limitasyncbottleneckrate-limiter-flexiblepromise-limit類似パッケージ:

Node.jsの非同期処理ライブラリ

非同期処理ライブラリは、Node.jsにおける非同期操作の管理を簡素化し、効率的なタスク実行を可能にします。これらのライブラリは、コールバックやプロミスを使用して非同期処理を行う際の複雑さを軽減し、タスクの実行順序や制限を設定するための便利な機能を提供します。これにより、開発者はより簡潔で可読性の高いコードを書くことができ、アプリケーションのパフォーマンスを向上させることができます。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
p-limit181,605,3732,82114.9 kB01ヶ月前MIT
async73,652,88028,196808 kB242年前MIT
bottleneck9,160,1591,978-897年前MIT
rate-limiter-flexible1,461,0173,489215 kB131日前ISC
promise-limit1,431,363143-108年前ISC

機能比較: p-limit vs async vs bottleneck vs rate-limiter-flexible vs promise-limit

タスク管理

  • p-limit:

    p-limitは、同時に実行するタスクの数を制限するシンプルな方法を提供します。これにより、システムリソースの過負荷を防ぎます。

  • async:

    asyncは、コールバックベースの非同期処理を簡素化するための多くのユーティリティ関数を提供します。これにより、タスクの直列実行や並列実行を簡単に制御できます。

  • bottleneck:

    bottleneckは、タスクの実行速度を制御するための強力なツールで、特定の時間内に実行できるタスクの数を制限することができます。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、APIリクエストやユーザーアクションのレート制限を柔軟に設定できる機能を提供します。

  • promise-limit:

    promise-limitは、プロミスを返す関数の実行を制御し、同時実行数を制限するための便利なライブラリです。

使用シナリオ

  • p-limit:

    軽量なタスク制限が必要な場合や、シンプルなプロミスベースの処理を行う場合に最適です。

  • async:

    非同期処理のフローを管理する必要がある場合や、複雑なコールバック地獄を避けたい場合に適しています。

  • bottleneck:

    APIの呼び出しやデータベースへのクエリの頻度を制限する必要がある場合に特に有用です。

  • rate-limiter-flexible:

    ユーザーごとのリクエスト制限や、特定の時間内に許可されるリクエスト数を設定したい場合に適しています。

  • promise-limit:

    プロミスを使用した非同期処理で、同時に実行するタスクの数を制限したい場合に役立ちます。

設計原則

  • p-limit:

    p-limitは、シンプルで直感的なAPIを提供し、非同期処理の制御を容易にします。

  • async:

    asyncは、コールバックのネストを避けるために、フロー制御を簡素化することを目的としています。

  • bottleneck:

    bottleneckは、タスクの実行を制御するための柔軟な設計を持ち、特定の条件に基づいてタスクをスロットリングします。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、柔軟なレート制限を実現するために、さまざまなストレージバックエンドをサポートしています。

  • promise-limit:

    promise-limitは、プロミスの特性を活かしつつ、同時実行数を制限することに特化しています。

拡張性

  • p-limit:

    p-limitは、シンプルなAPIを持ち、他のライブラリと組み合わせて使用することが容易です。

  • async:

    asyncは、さまざまなユーティリティ関数を提供しており、独自のタスク管理ロジックを追加することができます。

  • bottleneck:

    bottleneckは、カスタムのスロットリングロジックを実装するための拡張性を持っています。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、さまざまなストレージオプションをサポートしており、ニーズに応じて拡張可能です。

  • promise-limit:

    promise-limitは、プロミスの特性を活かしつつ、他の非同期処理ライブラリと組み合わせて使用できます。

学習曲線

  • p-limit:

    p-limitは、シンプルなAPIを持っているため、学習曲線が緩やかで、すぐに使い始めることができます。

  • async:

    asyncは、多くのユーティリティ関数を提供しているため、初めて使用する際には少し学習が必要ですが、使いこなすと非常に強力です。

  • bottleneck:

    bottleneckは、タスクの制御が直感的であり、比較的簡単に学ぶことができます。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、柔軟な設定が可能ですが、初めて使用する際には少し学習が必要です。

  • promise-limit:

    promise-limitは、プロミスに基づいているため、プロミスの基本を理解していればすぐに使えます。

選び方: p-limit vs async vs bottleneck vs rate-limiter-flexible vs promise-limit

  • p-limit:

    同時に実行するタスクの数を制限したい場合に選択します。シンプルで軽量な実装が特徴で、プロミスを使用した非同期処理に適しています。

  • async:

    非同期処理のフローを簡単に管理したい場合や、コールバックスタイルでの開発を好む場合に選択します。多くのユーティリティ関数が用意されており、タスクの並列実行や直列実行を簡単に制御できます。

  • bottleneck:

    タスクの実行速度や頻度を制御したい場合に選択します。特にAPIリクエストやデータベースクエリの制限を設ける必要がある場合に効果的です。

  • rate-limiter-flexible:

    リクエストのレート制限を柔軟に設定したい場合に選択します。特に、APIの使用制限やユーザーごとのリクエスト制限を行う際に役立ちます。

  • promise-limit:

    プロミスを使用して非同期処理を行う際に、同時実行数を制限したい場合に選択します。特に、プロミスを返す関数の実行を制御するのに便利です。

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 | object
Minimum: 1

Concurrency limit.

You can pass a number or an options object with a concurrency property.

rejectOnClear

Type: boolean
Default: false

Reject pending promises with an AbortError when clearQueue() is called. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

import pLimit from 'p-limit';

const limit = pLimit({concurrency: 1});

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.

Warning: Avoid calling the same limit function inside a function that is already limited by it. This can create a deadlock where inner tasks never run. Use a separate limiter for inner tasks.

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.

When rejectOnClear is enabled, pending promises are rejected with an AbortError. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

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.

rejectOnClear

Type: boolean
Default: false

Reject pending promises with an AbortError when clearQueue() is called. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

Recipes

See recipes.md for common use cases and patterns.

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…