npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qheap

v1.4.0

Published

binary heap priority queue

Downloads

25,855

Readme

qheap

Build Status Coverage Status

Qheap is a very fast classic heap / priority queue.

A heap is partially ordered balanced binary tree with the property that the value at the root comes before any value in either the left or right subtrees. It supports two operations, insert and remove, both O(log n) (and peek, O(1)). The tree can be efficiently mapped into an array: the root at offset [1] and each node at [n] having children Left at [2*n] and Right at [2*n + 1].

    var Heap = require('qheap');
    var h = new Heap();
    h.insert('c');
    h.insert('a');
    h.insert('b');
    h.remove();     // => 'a'

Api

new Heap( options )

create a new empty heap.

Options:

  • comparBefore: a fast comparison function that returns true when the first argument should be sorted before the second. This comparator runs much faster than the three-way compar below. The default is function(a,b) { return a < b }.

  • compar : comparison function to determine the item ordering. The function should return a value less than zero if the first argument should be ordered before the second (compatible with the function passed to sort()). The default ordering if no compar is specified is by <: function(a,b){ return a < b ? -1 : 1 }

  • freeSpace : when the heap shrinks to 1/4 its high-water mark, reallocate the storage space to free the unused memory, and reset the high-water mark. Default is false, avoiding the overhead of the array slice. Note: freeing space from the array halves the insert rate; use advisedly.

  • size : the initial capacity of the heap. The heap is extended as needed, but for debugging dumps it can be useful to specify a smaller starting size than the default 100.

If options is a function, it is taken to be the comparison function compar.

insert( item ), push( item ), enqueue( item )

insert the item into the heap and rebalance the tree. Item can be anything, only the compar function needs to know the actual type. Push is an alias for insert.

remove( ), shift( ), dequeue( )

remove and return the item at the root of the heap (the next item in the sequence), and rebalance the tree. When empty, returns undefined. Shift is an alias for remove.

peek( )

return the item at the root of the heap, but do not remove it. When empty, returns undefined.

length

the heap length property is the count of items currently in the heap. This is a read-only property, it must not be changed.

gc( [options] )

Resize the storage array to free all unused array slots. The options, if specified, control when to gc for more efficient operation.

Options:

  • minLength - do not resize arrays smaller than this cutoff. Default 0, resize even the smallest arrays.
  • minFull - do not resize arrays that are more full than this fraction. Default 1.00, resize unless 100% full.

Performance

Running the fastpriorityqueue benchmark:

function rand(i) {
    i = i + 10000;
    i = i ^ (i << 16);
    i = (i >> 5) ^ i ;
    return i & 0xFF;
}
function testLoop() {
    var i, b = new qheap();
    for (i = 0; i < 128; i++) { b.insert(rand(i)) }
    for (i = 128; i < 1280; i++) { b.insert(rand(i)) ; b.remove() }
}

new heap + 128 inserts + 1152 insert/removes
qtimeit=0.19.0 node=6.10.2 v8=5.1.281.98 platform=linux kernel=3.16.0-4-amd64 up_threshold=11
arch=ia32 mhz=4419 cpuCount=8 cpu="Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz"
name                  speed           rate
jsprioqueue           6,037 ops/sec   1000 >>>>>
heap                 29,913 ops/sec   4955 >>>>>>>>>>>>>>>>>>>>>>>>>
fastpriorityqueue    38,264 ops/sec   6338 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
qheap                41,139 ops/sec   6814 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Todo

  • might be more efficient to periodically gc the heap on a timer instead of checking on every remove

Related Work

  • heap - the classic, slow with custom comparator
  • qheap, this - very fast heap and priority queue
  • fastpriorityqueue - very fast, includes comprehensive list nodejs heaps
  • qlist - very fast circular buffer