Registry / testing / heap
library0.2.7jsnpmunverified

A binary heap (priority queue) implementation in CoffeeScript/JavaScript, ported from Python's heapq module. Current stable version is 0.2.7 (last released in 2013). No release cadence; maintenance-only mode. Key differentiator: simple API with both instance methods (push, pop, peek) and static methods for arrays, plus nlargest/nsmallest utilities. Alternatives like `flatqueue` or `tinyqueue` may offer better performance or modern features.

npm install heap
INSTALL
IMPORT
SIG · HEAP
H
heap
testingjavascriptv0.2.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Heap
var Heap = require('heap');
import Heap from 'heap';
This package is CommonJS, not ESM. Use require(). For TypeScript, you may need to use `const Heap = require('heap') as any;` if no types are available.
Heap constructor
var heap = new Heap();
var heap = Heap();
Must use `new` keyword to instantiate. Without `new`, the constructor will not return a Heap instance.
Heap.push
heap.push(item);
Heap.push(item);
push() is an instance method, not static. Static methods operate on arrays directly (e.g., Heap.push(array, item)).

Creates a min-heap using custom compare function, pushes elements, pops the smallest, and uses static nlargest to find top 3 from an array.

var Heap = require('heap'); var heap = new Heap(function(a, b) { return a - b; }); heap.push(3); heap.push(1); heap.push(2); var smallest = heap.pop(); console.log(smallest); // 1 var array = [1, 3, 4, 2, 5]; var largest3 = Heap.nlargest(array, 3); console.log(largest3); // [5, 4, 3]
Debug
Known issues
gotchaHeap.pop() on an empty heap returns undefined.
fix
Check heap.empty() before calling heap.pop() or handle undefined gracefully.
affects: >=0.0.0
gotchaStatic methods modify the array in place, not returning a new array.
fix
Be aware that heapify(array) changes the array directly. Clone first if needed.
affects: >=0.0.0
deprecatedPackage is not actively maintained; last release in 2013.
fix
Consider using an alternative like tinyqueue or flatqueue which are more modern and maintained.
affects: >=0.0.0
gotchaThe comparison function for custom heaps must return negative/positive/zero, not boolean like other JavaScript sort comparators.
fix
Use `function cmp(a, b) { return a - b; }` for min-heap, not `function cmp(a, b) { return a < b; }`.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: heap.push is not a function
Calling push() statically on the Heap class instead of on an instance.
fix
Create an instance first: var heap = new Heap(); then use heap.push(item);
ReferenceError: require is not defined
Using require() in an ESM environment or browser without bundling.
fix
In browser, use a script tag to include the heap.js file. In Node ESM, use createRequire or switch to an alternative package.
Heap.nlargest is not a function
Using an outdated version or incorrect import (e.g., default import vs named import).
fix
Ensure you are using require('heap') which returns the Heap class, and check package version supports nlargest/nsmallest (available since 0.2.0).
Upgrade
Version history
0.2.7latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
packageheap
heap — npm install heap · libregistry