DENQUE
Extremely fast and lightweight double-ended queue implementation with zero dependencies.
Double-ended queues can also be used as a:
This implementation is currently the fastest available, even faster than double-ended-queue
, see the benchmarks
Every queue operation is done at a constant O(1)
- including random access from .peekAt(index)
.
Works on all node versions >= v0.10
Quick Start
npm install denque
const Denque = require("denque");
const denque = new Denque([1,2,3,4]);
denque.shift();
denque.pop();
API
new Denque()
-> Denque
Creates an empty double-ended queue with initial capacity of 4.
var denque = new Denque();
denque.push(1, 2, 3);
denque.shift();
denque.pop();
#####new Denque(Array items)
-> Denque
Creates a double-ended queue from items
.
var denque = new Denque([1,2,3,4]);
denque.shift();
denque.pop();
#####push(item)
-> int
Push an item to the back of this queue. Returns the amount of items currently in the queue after the operation.
var denque = new Denque();
denque.push(1);
denque.pop();
denque.push(2);
denque.push(3);
denque.shift();
denque.shift();
#####unshift(item)
-> int
Unshift an item to the front of this queue. Returns the amount of items currently in the queue after the operation.
var denque = new Denque([2,3]);
denque.unshift(1);
denque.toString();
denque.unshift(-2);
denque.toString();
#####pop()
-> dynamic
Pop off the item at the back of this queue.
Note: The item will be removed from the queue. If you simply want to see what's at the back of the queue use peekBack()
or .peekAt(-1)
.
If the queue is empty, undefined
is returned. If you need to differentiate between undefined
values in the queue and pop()
return value -
check the queue .length
before popping.
var denque = new Denque([1,2,3]);
denque.pop();
denque.pop();
denque.pop();
denque.pop();
Aliases: removeBack
#####shift()
-> dynamic
Shifts off the item at the front of this queue.
Note: The item will be removed from the queue. If you simply want to see what's at the front of the queue use peekFront()
or .peekAt(0)
.
If the queue is empty, undefined
is returned. If you need to differentiate between undefined
values in the queue and shift()
return value -
check the queue .length
before shifting.
var denque = new Denque([1,2,3]);
denque.shift();
denque.shift();
denque.shift();
denque.shift();
#####toArray()
-> Array
Returns the items in the queue as an array. Starting from the item in the front of the queue and ending to the item at the back of the queue.
var denque = new Denque([1,2,3]);
denque.push(4);
denque.unshift(0);
denque.toArray();
#####peekBack()
-> dynamic
Returns the item that is at the back of this queue without removing it.
If the queue is empty, undefined
is returned.
var denque = new Denque([1,2,3]);
denque.push(4);
denque.peekBack();
#####peekFront()
-> dynamic
Returns the item that is at the front of this queue without removing it.
If the queue is empty, undefined
is returned.
var denque = new Denque([1,2,3]);
denque.push(4);
denque.peekFront();
#####peekAt(int index)
-> dynamic
Returns the item that is at the given index
of this queue without removing it.
The index is zero-based, so .peekAt(0)
will return the item that is at the front, .peekAt(1)
will return
the item that comes after and so on.
The index can be negative to read items at the back of the queue. .peekAt(-1)
returns the item that is at the back of the queue,
.peekAt(-2)
will return the item that comes before and so on.
Returns undefined
if index
is not a valid index into the queue.
var denque = new Denque([1,2,3]);
denque.peekAt(0);
denque.peekAt(1);
denque.peekAt(2);
denque.peekAt(-1);
denque.peekAt(-2);
denque.peekAt(-3);
Note: The implementation has O(1) random access using .peekAt()
.
Aliases: get
#####remove(int index, int count)
-> array
Remove number of items from the specified index from the list.
Returns array of removed items.
Returns undefined if the list is empty.
var denque = new Denque([1,2,3,4,5,6,7]);
denque.remove(0,3);
denque.remove(1,2);
var denque1 = new Denque([1,2,3,4,5,6,7]);
denque1.remove(4, 100);
100000 items in queue performance
denque.remove x 649,195 ops/sec ±1.33% (83 runs sampled)
native array splice x 54,461 ops/sec ±164.33% (11 runs sampled)
#####removeOne(int index)
-> dynamic
Remove and return the item at the specified index from the list.
Returns undefined if the list is empty.
var denque = new Denque([1,2,3,4,5,6,7]);
denque.removeOne(4);
denque.removeOne(3);
denque1.removeOne(1);
100000 items in queue performance
denque.removeOne x 487,168 ops/sec ±0.94% (85 runs sampled)
native array splice x 39,082 ops/sec ±0.87% (88 runs sampled)
#####splice(int index, int count, item1, item2, ...)
-> array
Native splice implementation.
Remove number of items from the specified index from the list and/or add new elements.
Returns array of removed items or empty array if count == 0.
Returns undefined if the list is empty.
var denque = new Denque([1,2,3,4,5,6,7]);
denque.splice(denque.length, 0, 8, 9, 10);
denque.toArray()
denque.splice(3, 3, 44, 55, 66);
denque.splice(5,4, 666,667,668,669);
denque.toArray()
100000 items in queue performance
denque.splice x 178,198 ops/sec ±8.68% (61 runs sampled)
native array splice x 3,639 ops/sec ±4.39% (65 runs sampled)
#####isEmpty()
-> boolean
Return true
if this queue is empty, false
otherwise.
var denque = new Denque();
denque.isEmpty();
denque.push(1);
denque.isEmpty();
#####clear()
-> void
Remove all items from this queue. Does not change the queue's capacity.
var denque = new Denque([1,2,3]);
denque.toString();
denque.clear();
denque.toString();
Benchmarks
1000 items in queue
denque x 31,015,027 ops/sec ±1.52% (86 runs sampled)
double-ended-queue x 21,350,509 ops/sec ±1.21% (86 runs sampled)
2 million items in queue
denque x 28,710,051 ops/sec ±0.95% (87 runs sampled)
double-ended-queue x 20,531,490 ops/sec ±1.04% (89 runs sampled)