p-limit vs async vs bottleneck vs rate-limiter-flexible vs promise-limit
"JavaScript 并发控制库"npm包对比
p-limitasyncbottleneckrate-limiter-flexiblepromise-limit类似的npm包:
什么是JavaScript 并发控制库?

这些库用于管理 JavaScript 中的异步操作,特别是在处理大量并发请求时。它们提供了不同的方式来限制并发操作的数量,以防止过载和提高性能。每个库都有其独特的功能和使用场景,适合不同的需求和开发者的偏好。

npm下载趋势
3 年
GitHub Stars 排名
统计详情
npm包名称
下载量
Stars
大小
Issues
发布时间
License
p-limit164,163,9782,65911.7 kB424 天前MIT
async68,600,36828,221808 kB201 年前MIT
bottleneck4,523,1481,947-876 年前MIT
rate-limiter-flexible1,363,7223,419192 kB173 天前ISC
promise-limit963,520143-107 年前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 提供灵活的速率限制功能,支持多种存储后端和复杂的限制策略,适合需要高度定制的场景。

  • promise-limit:

    promise-limit 在 Promise 的基础上实现并发控制,允许开发者限制 Promise 的并发执行,适合处理异步操作的场景。

使用场景

  • p-limit:

    适合简单的并发控制需求,如批量处理任务。

  • async:

    适合需要处理复杂异步流的应用,如数据处理、API 调用等。

  • bottleneck:

    适合需要控制请求速率的场景,如爬虫、API 请求等。

  • rate-limiter-flexible:

    适合需要灵活速率限制的应用,如用户请求限制、API 访问控制等。

  • promise-limit:

    适合需要在 Promise 中进行并发控制的场景,如异步文件读取等。

设计原则

  • p-limit:

    简单明了的设计,专注于并发数量的限制。

  • async:

    基于回调和控制流的设计,提供多种异步操作的组合方式。

  • bottleneck:

    基于速率限制的设计,专注于高效控制请求的数量和速率。

  • rate-limiter-flexible:

    灵活的设计,支持多种存储后端和复杂的限制策略。

  • promise-limit:

    在 Promise 的基础上进行设计,提供简单的 API 来控制并发。

学习曲线

  • p-limit:

    学习曲线非常平缓,简单易用,适合所有水平的开发者。

  • async:

    由于功能丰富,学习曲线相对较陡,适合有一定经验的开发者。

  • bottleneck:

    学习曲线较平缓,易于上手,适合新手和经验丰富的开发者。

  • rate-limiter-flexible:

    学习曲线相对较陡,适合需要复杂速率限制的开发者。

  • promise-limit:

    学习曲线较平缓,适合熟悉 Promise 的开发者。

性能

  • p-limit:

    性能良好,适合简单的并发控制需求。

  • async:

    在处理大量异步操作时,性能可能受到影响,需合理使用。

  • bottleneck:

    性能优越,能够高效控制请求速率,适合高并发场景。

  • rate-limiter-flexible:

    性能灵活,能够根据需求调整速率限制策略。

  • promise-limit:

    性能稳定,适合处理 Promise 的场景。

如何选择: 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 如果你需要一个灵活的速率限制器,支持多种存储后端(如内存、Redis等),并且需要复杂的速率限制策略。它适合需要高度定制化的速率限制需求。

  • promise-limit:

    选择 promise-limit 如果你希望在 Promise 的基础上进行并发控制,且需要简单的 API 来限制 Promise 的并发执行。它适合需要处理 Promise 的场景。

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…