Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bin-heap-js

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bin-heap-js

Simple Binary Heap implementation for JavaScript

  • 0.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

bin-heap-js

BinHeap

A binary heap with a simple native-array-based implementation.

Constructor BinHeap([cmp])

The constructor takes an optional comparator. If provided, the comparator should behave similar to the comparator passed to Array.prototype.sort.

Specifically:

If cmp(a, b) < 0, then a comes before b

If cmp(a, b) > 0, then b comes before a

Example

const BinHeap = require('bin-heap-js');

// Priority queue where a greater priority is more important
let priorityQueue = new BinHeap((a,b) => b.priority - a.priority); // max-heap
priorityQueue.push({name: "job C", job: ..., priority: 5});
priorityQueue.push({name: "job A", job: ..., priority: 99});
priorityQueue.push({name: "job B", job: ..., priority: 10});
process(priorityQueue.pop()); // job A
process(priorityQueue.pop()); // job B
process(priorityQueue.pop()); // job C

DEFAULT

The default comparator provided will work as a min-heap for numbers:

(a,b) => a - b

Methods

  • push(item) Push item onto heap
  • pop() Pop "top-most" item on heap
  • peek() Return "top-most" item on heap
  • size() Number of elements in the heap

FAQs

Package last updated on 25 Jul 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc