Peeking elements from a PriorityQueue using JavaScript

Peeking a PriorityQueue means getting the element with the highest priority without removing it from the queue. This operation is useful when you need to inspect the next item to be processed without actually processing it.

The peek() Method Implementation

The peek function returns the element with the highest priority. In our implementation, elements are stored in ascending order of priority, so the last element has the highest priority:

peek() {
    if (this.isEmpty()) {
        console.log("Queue Underflow!");
        return;
    }
    return this.container[this.container.length - 1];
}

Complete Example

Here's a complete working example showing how peek() works with a PriorityQueue:

class PriorityQueue {
    constructor(capacity = 10) {
        this.container = [];
        this.capacity = capacity;
    }
    
    isEmpty() {
        return this.container.length === 0;
    }
    
    enqueue(data, priority) {
        if (this.container.length >= this.capacity) {
            console.log("Queue Overflow!");
            return;
        }
        
        const element = { data, priority };
        let added = false;
        
        for (let i = 0; i < this.container.length; i++) {
            if (element.priority < this.container[i].priority) {
                this.container.splice(i, 0, element);
                added = true;
                break;
            }
        }
        
        if (!added) {
            this.container.push(element);
        }
    }
    
    peek() {
        if (this.isEmpty()) {
            console.log("Queue Underflow!");
            return;
        }
        return this.container[this.container.length - 1];
    }
    
    display() {
        console.log(this.container);
    }
}

// Test the peek() method
let q = new PriorityQueue(4);
q.enqueue("Hello", 3);
q.enqueue("World", 2);
q.enqueue("Foo", 8);

console.log("Peek result:", q.peek());
console.log("Queue contents:");
q.display();

// Peek again to show it doesn't remove the element
console.log("Peek again:", q.peek());
Peek result: { data: 'Foo', priority: 8 }
Queue contents:
[ { data: 'World', priority: 2 },
  { data: 'Hello', priority: 3 },
  { data: 'Foo', priority: 8 } ]
Peek again: { data: 'Foo', priority: 8 }

Key Differences: peek() vs dequeue()

Operation Returns Element? Removes Element? Queue Size Changes?
peek() Yes No No
dequeue() Yes Yes Yes

Common Use Cases

The peek() operation is commonly used when you need to:

  • Check the next task to process without starting it
  • Validate queue contents before performing operations
  • Implement conditional processing based on priority levels

Conclusion

The peek() method provides safe access to the highest priority element without modifying the queue structure. This makes it perfect for inspection and conditional processing scenarios.

Updated on: 2026-03-15T23:18:59+05:30

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements