Priority Queue in ES6 JavaScript

Priority Queue in ES6 JavaScript

I recently found myself trying to find a shortest path between two points in JavaScript. The approach I took was similar to Dijkstra's algorithm. A key component to the algorithm is the priority queue used to determine which edge should be evaluated next in the main loop.

In Python, the standard lib includes heapq but when I looked for something similar in JavaScript, I found a lot of overly complex solutions. In such cases, I usually just roll my own implementation.

class PriorityQueue {
  constructor() { = [];

  push(value, priority = 0) {
      value: value,
      priority: priority

  pop() {
    let index = 0;
    let min = Infinity;
    for (let i = 0; i <; i++) {
      let priority =[i].priority;
      if (Math.min(min, priority) === priority) {
        min = priority;
        index = i;
    return, 1)[0].value;

  size() {

Hopefully this is useful for someone else out there looking for the same thing. It's fairly straightforward to use. You can add arbitrary values using push(value, priority) and when you pop(), the item with the highest priority (lowest value) will be removed and returned.