Implement a Queue in JavaScript
Michael Mitrakos
4 min read
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach…
Implement a Queue in JavaScript
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
This is the second of a series of articles on implementing data structures in JavaScript. If you’re new to data structures, be sure to read this introduction (link to come) to data structures first. Already know about queues? Let’s get started on some more advanced data structures like the binary search tree!
A queue is like a line at a restaurant. It’s “first in, first out” (FIFO), which means that the item that was put in the queue longest ago is the first item that comes out. “First come, first served.”
The Problem
Implementing a Queue in JavaScript involves creating a data structure that follows the First-In-First-Out (FIFO) principle. To solve this problem, you need to design a class or a set of functions that provide the necessary operations for a Queue. The Queue should support adding elements to the back (enqueue) and removing elements from the front (dequeue). Additionally, it should allow for inspecting the front element (peek) without removing it. JavaScript provides various approaches to implement a Queue, such as using arrays or linked lists. By breaking down the problem, you can identify key components, such as maintaining a reference to the front and back of the Queue, updating these references when enqueueing or dequeueing elements, and handling edge cases like an empty Queue. The solution to this problem enables JavaScript developers to utilize the Queue data structure for managing ordered collections, scheduling tasks, or solving other problems that require FIFO behavior.
Most use-cases for a queue are involved with building or utilizing other data structures. One example could be in breadth-first traversal of a tree.
Methods of a Queue
Queues have two main methods:
- **enqueue() **: Adds a node (value)
- **dequeue() **: Removes and returns the next node in the queue
They can also include other utility methods:
- **peek() **: Returns the node at the front of the queue (without removing)
- **isEmpty() **: Returns True if the queue is empty, otherwise returns false
Pseudo Code
Create Queue constructor Define storage, count, and lowest count
Add value to queue
Increment count
Save node to delete in var
Delete node
Increment lowest count
Return saved node
Return count minus lowest count
Time complexity
If you don’t know much about time complexity, you’ll be lost until you read this short article on time complexity in JavaScript. For each method on the queue, the worst-case time complexity is constant — O(1). This means that as the stack grows to n size, each method completes its job in the same amount of time.
- enqueue: Constant — O(1)
- dequeue: Constant — O(1)
Code
- peek: Constant — O(1)
- isEmpty: Constant — O(1)
Are you ready to take on some more advanced data structures? Next up is the binary search tree!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
I founded Higglo Digital and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a beautifully designed website, check us out.
I also created Wanderlust Extension to discover the most beautiful places across the world with highly curated content. Check it out!