Efficient way to implement Priority Queue in Javascript?

前端 未结 3 775
一整个雨季
一整个雨季 2020-12-13 14:38

Priority Queues have a priority value and data, for every entry.

Thus, when adding a new element to the queue, it bubbles up to the surface if it has a higher priori

3条回答
  •  孤街浪徒
    2020-12-13 15:19

    I was not satisfied with the efficiency of existing priority queue implementations, so I decided to make my own:

    https://github.com/luciopaiva/heapify

    npm i heapify
    

    This will run faster than any other publicly known implementation due to the use of typed arrays.

    Works on both client and server ends, code base with 100% test coverage, tiny library (~100 LoC). Also, the interface is really simple. Here's some code:

    import Heapify from "heapify";
    
    const queue = new Heapify();
    queue.push(1, 10);  // insert item with key=1, priority=10
    queue.push(2, 5);  // insert item with key=2, priority=5
    queue.pop();  // 2
    queue.peek();  // 1
    queue.peekPriority();  // 10
    

提交回复
热议问题