• Stars
    star
    134
  • Rank 262,622 (Top 6 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created about 6 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A very fast and simple JavaScript priority queue

flatqueue

A very fast and tiny binary heap priority queue in JavaScript.

Similar to tinyqueue, but stores the queue as two flat arrays of items and their numeric priority values respectively (without a way to specify a comparator function). This makes the queue more limited, but several times faster.

Build Status Minzipped Size Simply Awesome

Usage

const q = new FlatQueue();

for (let i = 0; i < items.length; i++) {
    // Push an item index and its priority value. You can push other values as well,
    // but storing only integers is much faster due to JavaScript engine optimizations.
    q.push(i, items[i].priority);
}

q.peekValue(); // Read the top item's priority value
q.peek(); // Read the top item
q.pop(); // Remove and return the top item

Install

Install with npm install flatqueue, then use as a module:

import FlatQueue from 'flatqueue';

Alternatively, use as a module in a browser directly:

<script type="module">
    import FlatQueue from 'https://cdn.jsdelivr.net/npm/flatqueue/+esm';

There's also a UMD bundle that exposes a global FlatQueue global variable:

<script src="https://cdn.jsdelivr.net/npm/flatqueue"></script>

API

new FlatQueue()

Creates an empty queue object with the following methods and properties:

push(item, priority)

Adds item to the queue with the specified priority.

priority must be a number. Items are sorted and returned from low to high priority. Multiple items with the same priority value can be added to the queue, but the queue is not stable (items with the same priority are not guaranteed to be popped in iteration order).

pop()

Removes and returns the item from the head of this queue, which is one of the items with the lowest priority. If this queue is empty, returns undefined.

peek()

Returns the item from the head of this queue without removing it. If this queue is empty, returns undefined.

peekValue()

Returns the priority value of the item at the head of this queue without removing it. If this queue is empty, returns undefined.

clear()

Removes all items from the queue.

shrink()

Shrinks the internal arrays to this.length.

pop() and clear() calls don't free memory automatically to avoid unnecessary resize operations. This also means that items that have been added to the queue can't be garbage collected until a new item is pushed in their place, or this method is called.

length

Number of items in the queue. Read-only.

ids

An underlying array of items. Note that it can be bigger than the length as it's not eagerly cleared.

values

An underlying array of priority values. Note that it can be bigger than the length as it's not eagerly cleared.

Using typed arrays

If you know the maximum queue size beforehand, you can override the queue to use typed arrays for better performance and memory footprint. This makes it match the performance of the popular heapify library.

const q = new FlatQueue();
q.ids = new Uint16Array(32);
q.values = new Uint32Array(32);

More Repositories

1

suncalc

A tiny JavaScript library for calculating sun/moon positions and phases.
JavaScript
3,005
star
2

rbush

RBush β€” a high-performance JavaScript R-tree-based 2D spatial index for points and rectangles
JavaScript
2,374
star
3

simplify-js

High-performance JavaScript polyline simplification library
JavaScript
2,249
star
4

bullshit.js

A bookmarklet for translating marketing speak into human-readable text. πŸ’©
JavaScript
1,811
star
5

flatbush

A very fast static spatial index for 2D points and rectangles in JavaScript 🌱
JavaScript
1,378
star
6

simpleheat

A tiny JavaScript library for drawing heatmaps with Canvas
JavaScript
923
star
7

dead-simple-grid

Dead Simple Grid is a responsive CSS grid micro framework that is just that. Dead simple.
HTML
750
star
8

kdbush

A fast static index for 2D points
JavaScript
608
star
9

projects

A list of awesome open source projects Volodymyr Agafonkin is involved in.
398
star
10

tinyqueue

The smallest and simplest priority queue in JavaScript.
JavaScript
384
star
11

geokdbush

The fastest spatial index for geographic locations in JavaScript
JavaScript
332
star
12

road-orientation-map

A visualization of road orientations on an interactive map
JavaScript
296
star
13

robust-predicates

Fast robust predicates for computational geometry in JavaScript
JavaScript
290
star
14

rbush-knn

k-nearest neighbors search (KNN) for RBush
JavaScript
205
star
15

delaunator-rs

Fast 2D Delaunay triangulation in Rust. A port of Delaunator.
Rust
189
star
16

tinyjam

A radically simple, zero-configuration static site generator in JavaScript
JavaScript
149
star
17

quickselect

A fast selection algorithm in JavaScript.
JavaScript
82
star
18

seidel

[DEPRECATED] A JS polygon triangulation library
JavaScript
82
star
19

icomesh

Fast JavaScript icosphere mesh generation library for WebGL visualizations
JavaScript
51
star
20

bbtree

Self-balancing Binary Search Trees in JavaScript
JavaScript
49
star
21

yeahjs

A tiny, modern, fast EJS templating library
JavaScript
44
star
22

geoflatbush

Geographic kNN extension for Flatbush
JavaScript
43
star
23

kdbush.hpp

A fast static spatial index for 2D points in C++11
C++
32
star
24

worker-data-load

A test that shows the benefit of loading large amounts of data directly in a worker instead of a page.
JavaScript
32
star
25

Leaflet.TouchHover

A plugin for adding hover-like interaction to Leaflet maps on mobile devices
JavaScript
27
star
26

suncalc-go

SunCalc written in Go
Go
19
star
27

eslint-config-mourner

A strict ESLint config for my JavaScript projects
JavaScript
18
star
28

yeahml

A tiny subset of YAML for JavaScript
JavaScript
8
star
29

serenity-tm

Serenity is a light, minimal syntax highlighting theme for Sublime Text and Textmate.
8
star
30

pbf-split

Splits a Node stream of protocol buffer messages
JavaScript
7
star
31

hello-lib

Simple boilerplate for my small JS libraries.
JavaScript
7
star
32

color-metrics

JavaScript
6
star
33

agafonkin.com

My little personal page
HTML
5
star
34

fanny

A simple and fast multilayer feedforward neural network implementation in JS, made for learning purposes.
JavaScript
5
star
35

hain

Hain triangulation algorithm in JS (work in progress)
C++
4
star
36

mourner

2
star
37

mourner.github.com

Nothing here yet.
1
star