A queue data structure for Node.js and JavaScript.
Installation · Usage · API
Follow @marcuspoehls and @superchargejs for updates!
npm i @supercharge/queue-datastructure
This package is ESM-only.
Using the queue data structure is pretty straightforward. The library exposes a Queue
class that you can use to create a queue instance. You can create a queue from existing data or an empty one:
import { Queue } from '@supercharge/queue-datastructure'
// create a queue from an existing array
const queue = new Queue([ 1, 2, 3 ])
// or, create a queue from individual items
const queue = new Queue(1, 2, 3)
// or, create an empty queue
const queue = new Queue()
Push new items
to the end of the queue.
queue
.enqueue(1)
.enqueue(2, 3)
.enqueue([ 4, 5, 6])
// Queue: 1, 2, 3, 4, 5, 6
Remove and return the item which is up for processing. Returns undefined
if the queue is empty.
queue.enqueue(1, 2, 3)
queue.size() // 3
queue.dequeue() // 1
queue.size() // 2
Returns the first item without removing it from the queue. Returns undefined
if the queue is empty.
queue.enqueue(1, 2, 3)
queue.peek() // 1
Returns the number of items in the queue.
queue.size() // 0
queue.enqueue(1, 2)
queue.size() // 2
Returns true
if there are no items in the queue, false
otherwise.
queue.isEmpty() // true
queue.enqueue(1)
queue.isEmpty() // false
Returns true
if there are items in the queue, false
when the queue is empty.
queue.isNotEmpty() // false
queue.enqueue(1)
queue.isNotEmpty() // true
Removes all items from the queue.
queue.clear()
queue.size() // 0
- Create a fork
- Create your feature branch:
git checkout -b my-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request 🚀
MIT © Supercharge
superchargejs.com · GitHub @supercharge · Twitter @superchargejs