JSPM

zeno-queue

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q9742F
  • License MIT

Promise based queue with abort functionality

Package Exports

  • zeno-queue
  • zeno-queue/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (zeno-queue) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm version bundle size

ZenoQueue

A minimal, high-performance Promise-based queue.

Features

  • ~40% faster than array-based queues
  • Zero dependencies
  • Tiny (< 1kb)
  • Simple API
  • Promise chaining execution
  • AbortController cancellation

Installation

npm install zeno-queue

or

import { ZenoQueue } from 'https://unpkg.com/zeno-queue/index.js';

Usage

import { ZenoQueue } from 'zeno-queue';

const queue = new ZenoQueue();

// Queue an operation
queue(() => {
    console.log('First');
});

// Queue an async operation
queue(async () => {
    await someAsyncWork();
    console.log('Second');
});

// Cancel an operation before it starts
const task = queue(processData);
task.abort();

To cancel an operation that has already started you must access the .aborted boolean property of the context.

In order to prevent blocking during long running operations you should await .yield() after each iteration.

async function longRunningTask(context) {
    console.log('Starting');

    for (let i=0; i<10000; i++) {
        if (context.aborted) {
            console.log('Aborted');
            return;
        }

        console.log(i);
        await context.yield();
    }

    console.log('Done');
}

const task = queue(longRunningTask);
setTimeout(() => task.abort(), 1000);

Performance

ZenoQueue processes sequentially with O(1) complexity by chaining Promises rather than using traditional O(n) Array operations.

Here are the results of a test with 100,000 tasks:

ZenoQueue:   680.84ms
Array Queue: 1127.90ms

Limitations

For simple, speedy, sequential job queuing, ZenoQueue is the clear choice. But there are some aspects that may make it inappropriate for certain use-cases:

  • Tasks always execute in FIFO order
  • No pause/resume functionality
  • Task history must be tracked externally

Use a traditional array-based queue if you need:

  • Dynamic task priorities
  • Complex queue manipulation
  • Easy task tracking