Socket
Socket
Sign inDemoInstall

tinyqueue

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tinyqueue

The smallest and simplest JavaScript priority queue


Version published
Weekly downloads
1.6M
increased by6.28%
Maintainers
1
Install size
9.28 kB
Created
Weekly downloads
 

Package description

What is tinyqueue?

The tinyqueue npm package is a minimal priority queue implementation in JavaScript. It is designed to be very lightweight and fast, and it is often used for tasks that require a priority queue, such as pathfinding algorithms, scheduling, and event handling.

What are tinyqueue's main functionalities?

Creating a priority queue

This code sample demonstrates how to import the TinyQueue class and create a new priority queue instance.

const TinyQueue = require('tinyqueue');
const queue = new TinyQueue();

Adding items to the queue

This code sample shows how to add items to the queue with a specified priority. The lower the priority number, the sooner the item will be dequeued.

queue.push({ value: 'item1', priority: 1 });
queue.push({ value: 'item2', priority: 2 });

Removing the item with the highest priority

This code sample demonstrates how to remove and return the item with the highest priority (the item with the lowest priority number) from the queue.

const highestPriorityItem = queue.pop();

Peeking at the item with the highest priority without removing it

This code sample shows how to look at the item with the highest priority without removing it from the queue.

const highestPriorityItem = queue.peek();

Checking the size of the queue

This code sample demonstrates how to check the number of items in the queue.

const size = queue.length;

Other packages similar to tinyqueue

Readme

Source

tinyqueue

The smallest and simplest binary heap priority queue in JavaScript.

// create an empty priority queue
var queue = new TinyQueue();

// add some items
queue.push(7);
queue.push(5);
queue.push(10);

// remove the top item
var top = queue.pop(); // returns 5

// return the top item (without removal)
top = queue.peek(); // returns 7

// get queue length
queue.length; // returns 2

// create a priority queue from an existing array (modifies the array)
queue = new TinyQueue([7, 5, 10]);

// pass a custom item comparator as a second argument
queue = new TinyQueue([{value: 5}, {value: 7}], function (a, b) {
	return a.value - b.value;
});

// turn a queue into a sorted array
var array = [];
while (queue.length) array.push(queue.pop());

For a faster number-based queue, see flatqueue.

Install

Install using NPM (npm install tinyqueue) or Yarn (yarn add tinyqueue), then:

// import as an ES module
import TinyQueue from 'tinyqueue';

// or require in Node / Browserify
const TinyQueue = require('tinyqueue');

Or use a browser build directly:

<script src="https://unpkg.com/tinyqueue@2.0.0/tinyqueue.min.js"></script>

Thanks

Inspired by js-priority-queue by Adam Hooper.

Keywords

FAQs

Last updated on 14 Jun 2019

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