p-limit vs express-rate-limit vs limiter vs bottleneck vs rate-limiter-flexible vs ratelimiter
Node.js 限流库
p-limitexpress-rate-limitlimiterbottleneckrate-limiter-flexibleratelimiter类似的npm包:
Node.js 限流库

限流库用于控制应用程序的请求速率,以防止过载和滥用。它们通过限制单位时间内的请求数量来保护服务器,确保系统的稳定性和可用性。这些库适用于需要管理请求频率的场景,如 API 访问、爬虫防护和用户行为控制等。

npm下载趋势
3 年
GitHub Stars 排名
统计详情
npm包名称
下载量
Stars
大小
Issues
发布时间
License
p-limit180,627,2712,70211.7 kB52 个月前MIT
express-rate-limit10,750,9603,195141 kB81 个月前MIT
limiter9,850,4831,556158 kB141 年前MIT
bottleneck4,979,3921,959-876 年前MIT
rate-limiter-flexible1,467,6203,441191 kB1510 小时前ISC
ratelimiter115,657725-116 年前MIT
功能对比: p-limit vs express-rate-limit vs limiter vs bottleneck vs rate-limiter-flexible vs ratelimiter

功能丰富性

  • p-limit:

    p-limit 专注于控制并发操作的数量,适合 Promise 处理场景,功能相对单一。

  • express-rate-limit:

    express-rate-limit 提供了基本的速率限制功能,适合快速集成到 Express 应用中,但功能相对简单。

  • limiter:

    Limiter 是一个轻量级的库,功能简单,主要提供基本的速率限制。

  • bottleneck:

    Bottleneck 提供了多种限流策略,包括速率限制、并发限制和任务调度,适合复杂的应用场景。

  • rate-limiter-flexible:

    rate-limiter-flexible 提供了灵活的配置选项,支持多种存储后端,功能非常强大。

  • ratelimiter:

    ratelimiter 提供了基本的限流功能,适合小型项目,功能较为简单。

易用性

  • p-limit:

    p-limit 的 API 简洁明了,易于理解,适合快速实现并发控制。

  • express-rate-limit:

    express-rate-limit 的集成非常简单,几乎不需要额外的配置,适合快速上手。

  • limiter:

    Limiter 的使用非常简单,适合不需要复杂配置的用户。

  • bottleneck:

    Bottleneck 的 API 设计直观,易于使用,但由于功能丰富,可能需要一些时间来熟悉其所有选项。

  • rate-limiter-flexible:

    rate-limiter-flexible 的配置灵活,可能需要更多的学习和理解,但功能强大。

  • ratelimiter:

    ratelimiter 的使用非常直接,适合初学者。

性能

  • p-limit:

    p-limit 在处理并发请求时非常高效,能够有效控制并发数量。

  • express-rate-limit:

    express-rate-limit 性能良好,但在高并发情况下可能会遇到瓶颈。

  • limiter:

    Limiter 性能优秀,适合小型应用和简单的限流需求。

  • bottleneck:

    Bottleneck 在处理大量请求时表现良好,能够有效控制并发和速率,适合高负载场景。

  • rate-limiter-flexible:

    rate-limiter-flexible 设计用于高性能场景,能够处理大量请求,适合大规模应用。

  • ratelimiter:

    ratelimiter 性能一般,适合小型项目。

扩展性

  • p-limit:

    p-limit 主要用于控制并发,扩展性有限,适合特定场景。

  • express-rate-limit:

    express-rate-limit 的扩展性有限,主要用于基本的速率限制,适合简单需求。

  • limiter:

    Limiter 的扩展性较低,主要用于基本的限流功能。

  • bottleneck:

    Bottleneck 提供了丰富的扩展选项,能够根据需求进行自定义配置,适合复杂的应用场景。

  • rate-limiter-flexible:

    rate-limiter-flexible 提供了高度的扩展性,支持多种存储后端和配置选项,适合复杂需求。

  • ratelimiter:

    ratelimiter 的扩展性较低,适合简单应用。

适用场景

  • p-limit:

    适用于需要控制 Promise 并发的场景。

  • express-rate-limit:

    适用于基于 Express 的 API 限流,快速集成和使用。

  • limiter:

    适用于简单的速率限制需求,轻量级应用。

  • bottleneck:

    适用于需要复杂限流策略的场景,如任务调度和高并发请求控制。

  • rate-limiter-flexible:

    适用于需要高性能和灵活配置的复杂应用场景。

  • ratelimiter:

    适用于小型项目或简单的限流需求。

如何选择: p-limit vs express-rate-limit vs limiter vs bottleneck vs rate-limiter-flexible vs ratelimiter
  • p-limit:

    选择 p-limit 如果你需要控制并发操作的数量,特别是在处理 Promise 时,适合需要限制并发请求的场景。

  • express-rate-limit:

    选择 express-rate-limit 如果你在使用 Express 框架,并且需要一个简单易用的中间件来限制 API 请求速率,适合快速集成和使用。

  • limiter:

    选择 Limiter 如果你需要一个轻量级的限流库,适合简单的速率限制需求,且不需要复杂的配置和功能。

  • bottleneck:

    选择 Bottleneck 如果你需要一个功能丰富且灵活的限流解决方案,支持多种限流策略(如并发限制和速率限制),并且能够处理复杂的任务调度。

  • rate-limiter-flexible:

    选择 rate-limiter-flexible 如果你需要一个灵活的限流解决方案,支持多种存储后端(如 Redis 和 MongoDB),并且需要高性能和高可配置性的限流策略。

  • ratelimiter:

    选择 ratelimiter 如果你需要一个简单的限流实现,适合小型项目或不需要复杂功能的应用。

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…