Socket
Socket
Sign inDemoInstall

flatqueue

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    flatqueue

The smallest and simplest JavaScript priority queue


Version published
Weekly downloads
30K
increased by18.91%
Maintainers
1
Install size
8.55 kB
Created
Weekly downloads
 

Readme

Source

flatqueue Build Status

A very fast 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.

import FlatQueue from 'flatqueue';

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].value);
}

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

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.

Keywords

FAQs

Last updated on 29 Mar 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc