How to Implement Priority Queue in JavaScript Easily
A
priority queue in JavaScript can be implemented using a class with an internal array that keeps elements sorted by priority. You add items with a priority value and always remove the item with the highest priority first. This can be done by inserting elements in order or sorting the array after each insertion.Syntax
A priority queue can be implemented as a class with methods like enqueue(item, priority) to add items, and dequeue() to remove the highest priority item. Internally, it uses an array to store elements as objects with item and priority properties.
The key parts are:
enqueue(item, priority): Adds an element with a priority.dequeue(): Removes and returns the element with the highest priority.- Internal array keeps elements sorted by priority.
javascript
class PriorityQueue { constructor() { this.items = []; } enqueue(item, priority) { const element = { item, priority }; let added = false; for (let i = 0; i < this.items.length; i++) { if (priority < this.items[i].priority) { this.items.splice(i, 0, element); added = true; break; } } if (!added) { this.items.push(element); } } dequeue() { return this.items.shift(); } isEmpty() { return this.items.length === 0; } }
Example
This example shows how to create a priority queue, add items with different priorities, and remove them in priority order.
javascript
class PriorityQueue { constructor() { this.items = []; } enqueue(item, priority) { const element = { item, priority }; let added = false; for (let i = 0; i < this.items.length; i++) { if (priority < this.items[i].priority) { this.items.splice(i, 0, element); added = true; break; } } if (!added) { this.items.push(element); } } dequeue() { return this.items.shift(); } isEmpty() { return this.items.length === 0; } } const pq = new PriorityQueue(); pq.enqueue('Clean room', 2); pq.enqueue('Do homework', 1); pq.enqueue('Buy groceries', 3); while (!pq.isEmpty()) { const task = pq.dequeue(); console.log(`${task.item} (priority: ${task.priority})`); }
Output
Do homework (priority: 1)
Clean room (priority: 2)
Buy groceries (priority: 3)
Common Pitfalls
Common mistakes include:
- Not keeping the queue sorted, which breaks priority order.
- Using
pushwithout sorting or inserting in order. - Confusing higher priority with higher number (usually lower number means higher priority).
Always decide if lower or higher numbers mean higher priority and be consistent.
javascript
class WrongPriorityQueue { constructor() { this.items = []; } enqueue(item, priority) { // Just push without sorting this.items.push({ item, priority }); } dequeue() { // Removes first inserted, not highest priority return this.items.shift(); } } // Correct way is to insert in order or sort after enqueue as shown in previous example.
Quick Reference
Tips for implementing a priority queue in JavaScript:
- Use a class with an internal array.
- Insert elements in sorted order by priority.
- Lower priority number usually means higher priority.
- Use
shift()to remove the highest priority item. - Check if queue is empty before dequeueing.
Key Takeaways
Implement a priority queue using a class with an array sorted by priority.
Insert new elements in order to keep the highest priority item at the front.
Use dequeue to remove the item with the highest priority (lowest priority number).
Avoid just pushing items without sorting, or priority order will break.
Decide and be consistent if lower or higher numbers mean higher priority.