Skip to content

Latest commit

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

npm versionbundle 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';constqueue=newZenoQueue();// Queue an operationqueue(()=>{console.log('First');});// Queue an async operationqueue(async()=>{awaitsomeAsyncWork();console.log('Second');});// Cancel an operation before it startsconsttask=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.

asyncfunctionlongRunningTask(context){console.log('Starting');for(leti=0;i<10000;i++){if(context.aborted){console.log('Aborted');return;}console.log(i);awaitcontext.yield();}console.log('Done');}consttask=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

About

A minimal, high-performance Promise-based queue.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages