p-limit vs async vs bottleneck vs promise-limit vs rate-limiter-flexible
JavaScript 限制與佇列處理函式庫
p-limitasyncbottleneckpromise-limitrate-limiter-flexible類似套件:

JavaScript 限制與佇列處理函式庫

這些函式庫主要用於控制非同步操作的執行,特別是在需要限制並發數量或速率的情況下。它們提供了不同的功能來管理任務的執行順序、速率和佇列,幫助開發者更有效地處理大量的非同步請求,避免過載或資源浪費。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
p-limit461,131,1332,94316.1 kB02 個月前MIT
async146,162,03728,120808 kB472 年前MIT
bottleneck15,603,3862,009-897 年前MIT
promise-limit9,522,898142-108 年前ISC
rate-limiter-flexible4,759,0733,605234 kB124 天前ISC

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

速率限制

  • p-limit:

    p-limit 允許開發者設置同時執行的 Promise 數量,雖然不直接提供速率限制,但可以間接控制執行速率。

  • async:

    async 本身不提供速率限制功能,主要用於控制非同步流程的執行順序。

  • bottleneck:

    bottleneck 提供強大的速率限制功能,允許開發者設置每秒執行的最大函式數量,並支持佇列功能。

  • promise-limit:

    promise-limit 主要專注於限制 Promise 的並發執行數量,適合簡單的速率控制需求。

  • rate-limiter-flexible:

    rate-limiter-flexible 提供高度可配置的速率限制功能,支持多種存儲方式,適合需要跨請求進行限制的情況。

佇列管理

  • p-limit:

    p-limit 主要用於限制同時執行的 Promise 數量,並不提供完整的佇列管理功能。

  • async:

    async 提供了佇列功能,可以將任務按順序執行,適合需要確保任務執行順序的場景。

  • bottleneck:

    bottleneck 內建佇列管理,能夠在達到速率限制時自動將任務放入佇列,待速率允許時再執行。

  • promise-limit:

    promise-limit 提供簡單的佇列功能,能夠控制 Promise 的執行順序。

  • rate-limiter-flexible:

    rate-limiter-flexible 也支持佇列管理,能夠在達到速率限制時將請求放入佇列。

易用性

  • p-limit:

    p-limit 的 API 簡單明瞭,易於使用,特別適合需要基本並發控制的場景。

  • async:

    async 的 API 設計簡單易懂,適合初學者使用,並且有豐富的文檔支持。

  • bottleneck:

    bottleneck 提供清晰的 API 和詳細的文檔,易於上手,適合需要速率限制的開發者。

  • promise-limit:

    promise-limit 也有簡單的 API,適合小型專案或簡單的非同步操作。

  • rate-limiter-flexible:

    rate-limiter-flexible 的 API 雖然功能強大,但相對較複雜,適合有經驗的開發者使用。

性能

  • p-limit:

    p-limit 在限制並發數量方面性能良好,適合中小型專案使用。

  • async:

    async 在處理大量非同步任務時性能良好,但在高併發情況下可能會受到限制。

  • bottleneck:

    bottleneck 在高併發情況下表現出色,能夠有效控制函式的執行速率,避免過載。

  • promise-limit:

    promise-limit 在小型專案中性能良好,但在高併發情況下可能不如其他選擇。

  • rate-limiter-flexible:

    rate-limiter-flexible 提供高效的性能,特別是在需要跨多個請求進行限制時,能夠有效管理資源。

擴展性

  • p-limit:

    p-limit 的擴展性較低,主要用於基本的並發控制。

  • async:

    async 的擴展性有限,主要用於基本的非同步流程控制。

  • bottleneck:

    bottleneck 提供靈活的配置選項,能夠根據需求進行擴展,適合複雜的應用場景。

  • promise-limit:

    promise-limit 的擴展性有限,適合簡單的非同步操作。

  • rate-limiter-flexible:

    rate-limiter-flexible 提供高度的擴展性,支持多種存儲後端和配置選項,適合大型應用。

如何選擇: p-limit vs async vs bottleneck vs promise-limit vs rate-limiter-flexible

  • p-limit:

    選擇 p-limit 當你需要限制 Promise 的並發數量,並且希望簡單地控制同時執行的非同步操作數量。它的 API 簡單明瞭,適合需要基本並發控制的情況。

  • async:

    選擇 async 當你需要一個簡單的工具來處理非同步流程控制,並且希望使用回呼函式來管理任務執行時。它提供了多種控制流的功能,如串行、並行和佇列等,適合簡單的非同步任務管理。

  • bottleneck:

    選擇 bottleneck 當你需要強大的速率限制功能,並且希望能夠輕鬆地控制函式的執行速率和佇列。它提供了豐富的選項來設置速率限制,適合需要精細控制請求速率的場景。

  • promise-limit:

    選擇 promise-limit 當你需要一個輕量級的解決方案來限制 Promise 的並發執行。它專注於簡化 Promise 的佇列和執行,適合小型專案或簡單的非同步操作。

  • rate-limiter-flexible:

    選擇 rate-limiter-flexible 當你需要一個靈活且可擴展的速率限制解決方案,特別是在需要跨多個請求或用戶進行限制時。它支持多種存儲後端,適合需要高效能和可擴展性的應用。

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))).

If the iterable throws during iteration, the returned promise rejects with that error. Mapper functions that were already scheduled still run, and their results are discarded.

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 tear down 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().

limitedFunction.clearQueue()

Discard pending calls that have not started yet.

When rejectOnClear is enabled, pending promises are rejected with an AbortError. Otherwise, their promises remain unresolved.

This does not cancel calls that are already running.

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…