p-limit vs fastq vs async vs queue vs bottleneck vs promise-queue
비동기 제어 라이브러리
p-limitfastqasyncqueuebottleneckpromise-queue유사 패키지:

비동기 제어 라이브러리

비동기 제어 라이브러리는 JavaScript에서 비동기 작업을 효율적으로 관리하고 조정하기 위해 설계된 패키지입니다. 이러한 라이브러리는 비동기 작업의 실행 순서, 동시성 및 속도를 제어하여 성능을 최적화하고 코드의 가독성을 높입니다. 각 라이브러리는 특정한 사용 사례와 요구 사항에 맞춰 다양한 기능을 제공합니다.

npm 다운로드 트렌드

3 년

GitHub Stars 순위

통계 세부사항

패키지
다운로드
Stars
크기
Issues
발행일
라이선스
p-limit444,814,0492,93916.1 kB02ヶ月前MIT
fastq187,440,6351,14150.6 kB252ヶ月前ISC
async143,504,31928,117808 kB412年前MIT
queue23,927,71777019.6 kB194年前MIT
bottleneck15,123,5932,010-887年前MIT
promise-queue1,230,872230-109年前MIT

기능 비교: p-limit vs fastq vs async vs queue vs bottleneck vs promise-queue

비동기 흐름 제어

  • p-limit:

    p-limit는 동시 실행할 수 있는 비동기 작업의 수를 제한하여, 리소스를 효율적으로 사용할 수 있도록 합니다.

  • fastq:

    fastq는 작업을 큐에 추가하고, 지정된 동시성 수에 따라 작업을 처리합니다. 이를 통해 비동기 작업의 순서를 유지하면서도 효율적으로 처리할 수 있습니다.

  • async:

    async는 비동기 작업을 순차적으로 처리할 수 있도록 도와주며, 다양한 흐름 제어 메서드를 제공합니다. 이를 통해 복잡한 비동기 로직을 간단하게 구현할 수 있습니다.

  • queue:

    queue는 단순한 큐 구조를 제공하여, 작업을 순차적으로 실행할 수 있도록 도와줍니다.

  • bottleneck:

    bottleneck은 비동기 작업의 실행 속도를 제어할 수 있는 기능을 제공하여, 특정 시간 내에 실행할 수 있는 작업 수를 제한합니다.

  • promise-queue:

    promise-queue는 Promise를 기반으로 한 작업을 순차적으로 처리할 수 있는 기능을 제공하여, 비동기 작업의 흐름을 관리합니다.

속도 제어

  • p-limit:

    p-limit는 동시 실행 수를 제한하여, 비동기 작업의 속도를 제어하는 데 유용합니다.

  • fastq:

    fastq는 작업의 순서를 유지하면서도 동시성을 조절할 수 있어, 속도 제어에 유리합니다.

  • async:

    async는 비동기 작업의 흐름을 제어할 수 있지만, 속도 제한 기능은 내장되어 있지 않습니다. 사용자가 직접 구현해야 합니다.

  • queue:

    queue는 기본적인 큐 기능만 제공하므로, 속도 제어는 별도로 구현해야 합니다.

  • bottleneck:

    bottleneck은 속도 제한 기능이 내장되어 있어, API 호출과 같은 작업에서 유용하게 사용할 수 있습니다.

  • promise-queue:

    promise-queue는 Promise 기반의 작업을 순차적으로 처리하므로, 속도 제어는 사용자가 직접 관리해야 합니다.

사용 용이성

  • p-limit:

    p-limit는 매우 간단한 API를 제공하여, 동시 실행 수를 쉽게 제한할 수 있습니다.

  • fastq:

    fastq는 간단한 API로 비동기 작업을 큐에 추가하고 처리할 수 있어 사용이 용이합니다.

  • async:

    async는 다양한 메서드를 제공하지만, 콜백 패턴에 익숙하지 않은 경우 다소 복잡할 수 있습니다.

  • queue:

    queue는 기본적인 큐 기능만 제공하므로, 사용이 간단하지만 기능이 제한적입니다.

  • bottleneck:

    bottleneck은 직관적인 API를 제공하여, 속도 제한이 필요한 작업을 쉽게 구현할 수 있습니다.

  • promise-queue:

    promise-queue는 Promise 기반의 작업을 처리하는 데 최적화되어 있어, Promise에 익숙한 개발자에게 적합합니다.

성능

  • p-limit:

    p-limit는 동시 실행 수를 제한하여, 리소스를 효율적으로 사용할 수 있습니다.

  • fastq:

    fastq는 동시성을 유지하면서도 작업을 순차적으로 처리할 수 있어 성능이 뛰어납니다.

  • async:

    async는 다양한 비동기 작업을 처리할 수 있지만, 복잡한 로직으로 인해 성능 저하가 발생할 수 있습니다.

  • queue:

    queue는 기본적인 큐 기능을 제공하므로, 성능은 간단하지만 제한적입니다.

  • bottleneck:

    bottleneck은 속도 제한을 통해 성능을 최적화할 수 있으며, 리소스를 효율적으로 사용할 수 있습니다.

  • promise-queue:

    promise-queue는 Promise 기반의 작업을 순차적으로 처리하므로, 성능이 안정적입니다.

유연성

  • p-limit:

    p-limit는 동시 실행 수를 쉽게 조절할 수 있어 유연하게 사용할 수 있습니다.

  • fastq:

    fastq는 작업의 순서를 유지하면서도 동시성을 조절할 수 있어 유연성이 높습니다.

  • async:

    async는 다양한 비동기 흐름 제어를 지원하여 유연하게 사용할 수 있습니다.

  • queue:

    queue는 기본적인 큐 기능을 제공하므로, 유연성은 제한적입니다.

  • bottleneck:

    bottleneck은 속도 제한 기능을 통해 유연하게 비동기 작업을 조절할 수 있습니다.

  • promise-queue:

    promise-queue는 Promise 기반의 작업을 처리하는 데 최적화되어 있어 유연성이 높습니다.

선택 방법: p-limit vs fastq vs async vs queue vs bottleneck vs promise-queue

  • p-limit:

    p-limit는 비동기 작업의 동시성을 제어하는 간단한 방법을 제공합니다. 동시 실행 수를 제한하고 싶을 때 선택하세요.

  • fastq:

    fastq는 큐 기반의 비동기 작업 처리에 적합하며, 작업을 순차적으로 처리하면서도 동시성을 유지할 수 있습니다. 작업의 순서가 중요한 경우 유용합니다.

  • async:

    async는 다양한 비동기 흐름 제어를 지원하며, 복잡한 비동기 작업을 처리하는 데 유용합니다. 콜백 패턴에 익숙한 개발자에게 적합합니다.

  • queue:

    queue는 기본적인 큐 기능을 제공하며, 작업을 순차적으로 실행하는 데 유용합니다. 간단한 비동기 작업을 처리할 때 적합합니다.

  • bottleneck:

    bottleneck은 비동기 작업의 속도를 제어하고, 동시 실행 수를 제한하는 데 최적화되어 있습니다. API 호출과 같은 작업에서 속도 제한이 필요한 경우 선택하세요.

  • promise-queue:

    promise-queue는 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 | 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…