npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

bounded-queue

v0.1.5

Published

Bounded batch queue, where items are produced and consumed based on user specified functions

Downloads

8

Readme

CI NPM version npm downloads

bounded-queue

bounded-queue helps solves the producer–consumer problem.

graph LR;
    P(Producer);
    B[bounded-queue];
    C(Consumer);
    P-- batched item -->B;
    B-- batched item -->C;
    style B fill:#99E,stroke:#333

The bounded-queue allows the producer and consumer to operate in

Introduction

Imagine you have to read records from a database and write those to another database. A simple way to that move the records is to first read from database A and sequentially write each record to database B.

let batchNr = 0;
let items2produce = 10;

/**
 * Mockup database A, producer
 */
const dbA = {
    readRecord: async () => {
        ++batchNr;
        console.log("Producing batch #", batchNr);
        await new Promise(resolve => setTimeout(resolve, 100));
        console.log("Produced  batch #", batchNr);
        return batchNr;
    },
    moreRecordsAvailable: () => batchNr < items2produce
}

/**
 * Mockup database B, consumer
 */
const dbB = {
    async writeRecord(batchNr) {
        console.log("Consuming batch #", batchNr);
        await new Promise(resolve => setTimeout(resolve, 100));
        console.log("Consumed  batch #", batchNr);
    }
}

/**
 * Sequential conversion
 */
async function convertDatabaseRecords() {

    while(dbA.moreRecordsAvailable()) {
        const record = await dbA.readRecord();
        // Consumer
        await dbB.writeRecord(record); // expensive async write (consume) operation
    }
}

(async () => {
    console.time("no-queue");
    await convertDatabaseRecords();
    console.timeEnd("no-queue");
})();

In the previous example, we either read from database A, or write to database B. It would be faster if read from database A, while we write to database B, at the same time. As dbA.readRecord() and dbB.readRecord()areasync` functions, there is no need to introduce threading to accomplish that.

The bounded-queue helps you with that. The following example uses bounded-queue, with a maximum of 3 queued records:

import {queue} from 'bounded-queue';

let batchNr = 0;
let items2produce = 10;

/**
 * Mockup database A, producer
 */
const dbA = {
    readRecord: async () => {
        ++batchNr;
        console.log("Producing batch #", batchNr);
        await new Promise(resolve => setTimeout(resolve, 100));
        console.log("Produced  batch #", batchNr);
        return batchNr;
    },
    moreRecordsAvailable: () => batchNr < items2produce
}

/**
 * Mockup database B, consumer
 */
const dbB = {
    async writeRecord(batchNr) {
        console.log("Consuming batch #", batchNr);
        await new Promise(resolve => setTimeout(resolve, 100));
        console.log("Consumed  batch #", batchNr);
    }
}

/**
 * Conversion using bounded-queue
 */
async function convertDatabaseRecords() {

    await queue(3, () => {
        // Producer
        return dbA.moreRecordsAvailable() ? dbA.readRecord() : null; // expenive async read (produce) operation
    }, record => {
        // Consumer
        return dbB.writeRecord(record); // expensive async write (consume) operation
    });
}

(async () => {
    console.time("bounded-queue");
    await convertDatabaseRecords();
    console.timeEnd("bounded-queue");
})();

Using the bounded-queue, the conversion will complete in roughly half the time.

Installation

npm install bounded-queue

API Documentation

BoundedQueue class

Constructor

constructor(maxQueueSize: number, producer: Producer<ItemType>, consumer: Consumer<ItemType>)

  • Parameters:
    • maxQueueSize (number): Maximum number of items that can be in the queue.
    • producer (Producer): A function that produces items to be added to the queue.
    • consumer (Consumer): A function that consumes items from the queue.
  • Description: Initializes a new BoundedQueue instance with the specified maxQueueSize, producer, and consumer.

Methods

length(): number

  • Description: Returns the number of items currently in the queue.
  • Returns: The number of items in the queue.

run(): Promise<void>

  • Description: Initiates the asynchronous processing of items in the queue. It starts filling the queue with items produced by the producer function and concurrently consumes items using the consumer function.
  • Returns: A Promise that resolves when all items have been produced and consumed.

Example Usage

import { queue } from 'your-module';

// Create and run a BoundedQueue with a maximum queue size of 10
queue(10, producer, consumer)
  .then(() => {
    console.log('Queue processing completed');
  })
  .catch((error) => {
    console.error('Error during queue processing:', error);
  });

Classless usage

queue() Function

queue<ItemType>(maxQueueSize: number, producer: Producer<ItemType>, consumer: Consumer<ItemType>): Promise<void>

  • Parameters:
    • maxQueueSize (number): Maximum number of items that can be in the queue.
    • producer (Producer): A function that produces items to be added to the queue.
    • consumer (Consumer): A function that consumes items from the queue.
  • Description: A convenience function for creating and running a BoundedQueue instance. It takes the same parameters as the BoundedQueue constructor and returns a Promise that resolves when all items have been produced and consumed.
  • Returns: A Promise that resolves when all items have been produced and consumed. Resolving null indicated the end of the production.

Example Usage

import { queue } from 'your-module';

// Create and run a BoundedQueue with a maximum queue size of 10
queue(10, producer, consumer)
  .then(() => {
    console.log('Queue processing completed');
  })
  .catch((error) => {
    console.error('Error during queue processing:', error);
  });