Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
denque
Advanced tools
Package description
The denque npm package is a fast double-ended queue implementation. It is optimized for performance and is useful for situations where you need a queue that allows adding or removing items from both ends.
Adding items
This demonstrates how to add items to the queue. 'push' adds an item to the end of the queue, while 'unshift' adds an item to the beginning.
const Denque = require('denque');
let myQueue = new Denque();
myQueue.push('item1');
myQueue.unshift('item0');
Removing items
This shows how to remove items from the queue. 'pop' removes an item from the end of the queue, and 'shift' removes an item from the beginning.
const removedLast = myQueue.pop();
const removedFirst = myQueue.shift();
Peeking at items
This code sample demonstrates how to peek at items in the queue without removing them. 'peekFront' looks at the first item, and 'peekBack' looks at the last item.
const firstItem = myQueue.peekFront();
const lastItem = myQueue.peekBack();
FastQueue is another high-performance queue implementation. It offers similar functionality to Denque but may have different performance characteristics under certain conditions.
This package also provides a double-ended queue implementation. It is similar to Denque in terms of API and functionality but might have slight differences in performance or additional methods.
Readme
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
npm install denque
const Denque = require("denque");
const denque = new Denque([1,2,3,4]);
denque.shift(); // 1
denque.pop(); // 4
new Denque()
new Denque(Array items)
push(item)
unshift(item)
pop()
shift()
toArray()
peekBack()
peekFront()
peekAt(int index)
remove(int index, int count)
removeOne(int index)
splice(int index, int count, item1, item2, ...)
isEmpty()
clear()
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(); //1
denque.pop(); //3
new Denque(Array items)
-> Denque
Creates a double-ended queue from items
.
var denque = new Denque([1,2,3,4]);
denque.shift(); // 1
denque.pop(); // 4
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(); // 1
denque.push(2);
denque.push(3);
denque.shift(); // 2
denque.shift(); // 3
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(); // "1,2,3"
denque.unshift(-2);
denque.toString(); // "-2,-1,0,1,2,3"
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(); // 3
denque.pop(); // 2
denque.pop(); // 1
denque.pop(); // undefined
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(); // 1
denque.shift(); // 2
denque.shift(); // 3
denque.shift(); // undefined
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(); // [0,1,2,3,4]
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(); // 4
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(); // 1
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); //1
denque.peekAt(1); //2
denque.peekAt(2); //3
denque.peekAt(-1); // 3
denque.peekAt(-2); // 2
denque.peekAt(-3); // 1
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); //[1,2,3]
denque.remove(1,2); //[5,6]
var denque1 = new Denque([1,2,3,4,5,6,7]);
denque1.remove(4, 100); //[5,6,7]
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); // 5
denque.removeOne(3); // 4
denque1.removeOne(1); // 2
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() // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
denque.splice(3, 3, 44, 55, 66); // [4,5,6]
denque.splice(5,4, 666,667,668,669); // [ 66, 7, 8, 9 ]
denque.toArray() // [ 1, 2, 3, 44, 55, 666, 667, 668, 669, 10 ]
isEmpty()
-> boolean
Return true
if this queue is empty, false
otherwise.
var denque = new Denque();
denque.isEmpty(); // true
denque.push(1);
denque.isEmpty(); // false
clear()
-> void
Remove all items from this queue. Does not change the queue's capacity.
var denque = new Denque([1,2,3]);
denque.toString(); // "1,2,3"
denque.clear();
denque.toString(); // ""
Darwin 17.0.0 x64
Node.JS 9.4.0
V8 6.2.414.46-node.17
Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz × 8
(3 x shift + 3 x push ops per 'op')
denque x 64,365,425 ops/sec ±0.69% (92 runs sampled)
double-ended-queue x 26,646,882 ops/sec ±0.47% (94 runs sampled)
(3 x shift + 3 x push ops per 'op')
denque x 61,994,249 ops/sec ±0.26% (95 runs sampled)
double-ended-queue x 26,363,500 ops/sec ±0.42% (91 runs sampled)
(1 x splice per 'op') - initial size of 100,000 items
denque.splice x 925,749 ops/sec ±22.29% (77 runs sampled)
native array splice x 7,777 ops/sec ±8.35% (50 runs sampled)
(1 x remove + 10 x push per 'op') - initial size of 100,000 items
denque.remove x 2,635,275 ops/sec ±0.37% (95 runs sampled)
native array splice - Fails to complete: "JavaScript heap out of memory"
(1 x removeOne + 10 x push per 'op') - initial size of 100,000 items
denque.removeOne x 1,088,240 ops/sec ±0.21% (93 runs sampled)
native array splice x 5,300 ops/sec ±0.41% (96 runs sampled)
FAQs
The fastest javascript implementation of a double-ended queue. Used by the official Redis, MongoDB, MariaDB & MySQL libraries for Node.js and many other libraries. Maintains compatability with deque.
The npm package denque receives a total of 8,038,028 weekly downloads. As such, denque popularity was classified as popular.
We found that denque demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.