
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
js-heap is a flexible, value-agnostic heap for Javascript. It provides a simple interface and allows users to define custom comparison functions for heap sorting.
npm install js-heap
To get started, require
js-heap, and create a new Heap instance:
var Heap = require('js-heap');
var h = new Heap(function(a,b) {
return a - b;
});
The comparison function behaves like a sort function you would pass into a standard Javascript Array.
The default comparison function is the one shown above - the heap is organized so that the highest numeric priority is at the top.
A node in the heap can be whatever you'd like:
// Example node
var node = {
priority: 1,
value: 'ABCD',
}
h.push(node);
You can create a custom sort function to compare arbitrary nodes:
var h = new Heap(function(a, b) {
return a.priority - b.priority;
});
##.push(node) Pushes a new node onto the heap. A "node" value can be a primitive or an arbitrary Javascript object. Pushed nodes are sorted based on the provided sort function.
h.push(1);
h.push(2);
...
##.pop()
Removes and returns the top node in the heap. If the heap is empty, null
is returned.
// Returns 2
h.pop();
// Returns 1
h.pop();
// Returns null
h.pop();
...
##.peek() Returns the top node in the heap. The node remains on top of the heap.
h.push(1);
// Returns 1
h.peek();
// Still returns 1
h.peek();
...
##.isEmpty()
Returns true
if the heap is empty.
##.length A read-only length property representing the number of nodes in the heap.
FAQs
A javascript heap implementation
The npm package js-heap receives a total of 91 weekly downloads. As such, js-heap popularity was classified as not popular.
We found that js-heap demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.