What is fast-fifo?
The fast-fifo npm package is a high-performance, zero-dependency queue implementation for JavaScript. It is designed to be a fast FIFO (first-in-first-out) queue structure that can be used in various scenarios where queue operations are required, such as task scheduling, event handling, or data processing pipelines.
What are fast-fifo's main functionalities?
Queue Creation
This feature allows for the creation of a new queue instance. The Queue class is imported from the fast-fifo package and instantiated to create a new queue.
const Queue = require('fast-fifo');
const q = new Queue();
Enqueue
This feature is used to add an item to the end of the queue. The push method is called on the queue instance with the item to be added as an argument.
q.push('some data');
Dequeue
This feature is used to remove and return the item at the front of the queue. The shift method is called on the queue instance to dequeue the item.
const item = q.shift();
Peek
This feature allows you to look at the item at the front of the queue without removing it. The head method is called on the queue instance to retrieve the first item.
const firstItem = q.head();
Queue Length
This feature provides the current number of items in the queue. The length property of the queue instance gives the count of items.
const queueLength = q.length;
Other packages similar to fast-fifo
queue-microtask
This package is similar to fast-fifo in that it provides a queue mechanism, but it is specifically designed for queuing microtasks within the same tick of the event loop, rather than for general FIFO queue data structures.
p-queue
p-queue is a promise-based queue with concurrency control. It is similar to fast-fifo in managing tasks in a queue, but it also allows for setting concurrency limits and prioritizing tasks.
bee-queue
bee-queue is a simple, fast, robust job/task queue for Node.js, backed by Redis. It is similar to fast-fifo in that it manages a queue of tasks, but it is more feature-rich, including persistence, retries, and job events.
async.queue
Part of the async utility module, async.queue provides a queue implementation that can process tasks with configurable concurrency. It is similar to fast-fifo in managing tasks in a queue but focuses on asynchronous task processing.
fast-fifo
A fast fifo implementation similar to the one powering nextTick in Node.js core
npm install fast-fifo
Uses a linked list of growing fixed sized arrays to implement the FIFO to avoid
allocating a wrapper object for each item.
Usage
const FIFO = require('fast-fifo')
const q = new FIFO()
q.push('hello')
q.push('world')
q.shift()
q.shift()
API
q = new FIFO()
Create a new FIFO.
q.push(value)
Push a value to the FIFO. value
can be anything other than undefined.
value = q.shift()
Return the oldest value from the FIFO.
bool = q.isEmpty()
Returns true
if the FIFO is empty and false otherwise.
Benchmarks
Included in bench.js is a simple benchmark that benchmarks this against a simple
linked list based FIFO.
On my machine the benchmark looks like this:
fifo bulk push and shift: 2881.508ms
fifo individual push and shift: 3248.437ms
fast-fifo bulk push and shift: 1606.972ms
fast-fifo individual push and shift: 1328.064ms
fifo bulk push and shift: 3266.902ms
fifo individual push and shift: 3320.944ms
fast-fifo bulk push and shift: 1858.307ms
fast-fifo individual push and shift: 1516.983ms
YMMV
License
MIT