nolanlawson / tiny-queue

Simple JavaScript FIFO queue implementation to avoid having to do shift()

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tiny-queue

A simple FIFO queue implementation as a linked list. The main benefit is to avoid doing shift() on an array, which may be slow. It's implemented in the straightforward root -> node1 -> node2 -> etc. architecture that you may have learned in CS 101.

This can typically be used as a drop-in replacement for an array, and it's only 38 lines of code.

See this Wikipedia page for a good explanation of the tradeoffs of a linked list versus other data structures.

Status

browser support

Usage

npm install tiny-queue

Then:

var Queue = require('tiny-queue');
var queue = new Queue();

queue.push('foo');
queue.push('bar');
queue.shift(); // 'foo'
queue.shift(); //'bar'
queue.length; // 0
queue.shift(); // undefined

API

The returned Queue object, once instantiated, only supports four operations:

queue.push()
queue.shift()
queue.slice() // returns a regular Array
queue.length

So it's basically a drop-in replacement for most naïve usages of an array as a queue.

About

Simple JavaScript FIFO queue implementation to avoid having to do shift()

License:Apache License 2.0


Languages

Language:JavaScript 100.0%