p-limit vs express-rate-limit vs limiter vs bottleneck vs rate-limiter-flexible vs ratelimiter
ウェブ開発におけるレートリミッターライブラリ
p-limitexpress-rate-limitlimiterbottleneckrate-limiter-flexibleratelimiter類似パッケージ:
ウェブ開発におけるレートリミッターライブラリ

レートリミッターライブラリは、アプリケーションが特定の時間内に行うリクエストの数を制限するために使用されます。これにより、サーバーの過負荷を防ぎ、APIの使用を管理し、悪意のある攻撃から保護することができます。これらのライブラリは、特に高トラフィックの環境や、リソースを効率的に管理する必要がある場合に重要です。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
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は、HTTPリクエストに対して簡単にレート制限を設定できる機能を提供します。特定のルートやメソッドに対して異なる制限を設定でき、ミドルウェアとして簡単に統合可能です。

  • limiter:

    Limiterは、基本的なレート制限機能を提供します。シンプルなAPIで、特定の時間内に許可されるリクエスト数を設定できます。

  • bottleneck:

    Bottleneckは、タスクの実行を制御するための多くの機能を提供します。特定の時間内に実行できるタスクの数を制限し、バースト処理をサポートします。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、複雑なレート制限を設定できる柔軟性を持っています。IPアドレスやユーザーIDに基づいて制限を設定でき、Redisなどの外部ストレージを使用することも可能です。

  • ratelimiter:

    ratelimiterは、シンプルなレート制限機能を提供し、特に小規模なアプリケーションに適しています。使いやすく、すぐに実装できます。

設定の柔軟性

  • p-limit:

    p-limitは、Promiseの同時実行数を制限するためのシンプルな設定が可能で、柔軟性があります。

  • express-rate-limit:

    express-rate-limitは、各ルートに対して異なる制限を設定できるため、柔軟性がありますが、全体的にはシンプルな設定です。

  • limiter:

    Limiterは、基本的な設定が可能ですが、他のライブラリと比べると柔軟性は低いです。

  • bottleneck:

    Bottleneckは、タスクの実行間隔や最大同時実行数を細かく設定できるため、非常に柔軟です。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、非常に柔軟な設定が可能で、さまざまな条件に基づいて制限を設定できます。

  • ratelimiter:

    ratelimiterは、シンプルな設定で使いやすいですが、柔軟性は他のライブラリに比べて低いです。

パフォーマンス

  • p-limit:

    p-limitは、Promiseの処理を効率的に管理するため、パフォーマンスが高いです。

  • express-rate-limit:

    express-rate-limitは、ミドルウェアとして非常に軽量で、パフォーマンスに優れています。

  • limiter:

    Limiterは、シンプルな実装のため、パフォーマンスが良好です。

  • bottleneck:

    Bottleneckは、タスクの実行を効率的に管理し、過負荷を防ぐための最適化がされています。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、Redisなどを使用することで、スケーラブルなパフォーマンスを提供します。

  • ratelimiter:

    ratelimiterは、シンプルな実装のため、パフォーマンスが良好ですが、大規模なシステムには向いていないかもしれません。

使用シナリオ

  • p-limit:

    p-limitは、Promiseベースのタスクを効率的に管理したい場合に最適です。

  • express-rate-limit:

    express-rate-limitは、ExpressアプリケーションでのHTTPリクエスト制限に最適です。

  • limiter:

    Limiterは、基本的なレート制限が必要な場合に適しています。

  • bottleneck:

    Bottleneckは、APIリクエストの制御や、複数の非同期タスクを管理する際に特に有用です。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、複雑なレート制限が必要な場合や、分散システムでの使用に適しています。

  • ratelimiter:

    ratelimiterは、小規模なアプリケーションやシンプルな実装が必要な場合に適しています。

学習曲線

  • p-limit:

    p-limitは、Promiseに特化しているため、Promiseの理解があれば簡単に学習できます。

  • express-rate-limit:

    express-rate-limitは、シンプルなAPIのため、学習が容易です。

  • limiter:

    Limiterは、非常にシンプルで、すぐに使い始めることができます。

  • bottleneck:

    Bottleneckは、多機能であるため、学習曲線がやや急ですが、使いこなすことで強力な制御が可能です。

  • rate-limiter-flexible:

    rate-limiter-flexibleは、柔軟性が高いため、学習には少し時間がかかるかもしれませんが、強力な機能を提供します。

  • ratelimiter:

    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アプリケーションで簡単に使用できるシンプルなレートリミッターを探している場合は、express-rate-limitが適しています。ミドルウェアとして簡単に統合でき、特定のルートに対して制限を設定できます。

  • limiter:

    Limiterは、シンプルで軽量なレートリミッターを提供します。特に、基本的なリクエスト制限が必要な場合に適しており、設定が簡単です。

  • bottleneck:

    Bottleneckは、複雑なリクエスト制御が必要な場合に最適です。特に、複数のタスクを同時に実行し、各タスクの実行速度を調整したい場合に有用です。

  • rate-limiter-flexible:

    柔軟な設定が可能なレートリミッターを必要とする場合は、rate-limiter-flexibleが適しています。Redisなどのストレージを使用して、分散環境での利用にも対応しています。

  • 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…