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

@oresoftware/linked-queue

v2.1.128

Published

Synchronous queue implementation with constant/linear time operations.

Downloads

668

Readme

@oresoftware / linked-queue

Constant time queue

This an implementation of a FIFO queue, where all methods/operations are constant in time complexity.

The key proposition of this library is that it will allow you to remove or insert items from/in the middle of the queue, in constant time. It also allows you to add/remove items to/from the front of the queue in constant time. Whereas, Array.prototype.shift/Array.prototype.unshift are O(N), because they have to update the indices of the entire array, etc.

Because any FIFO queue implementation using a basic JavaScript array will need to call either Array.prototype.shift or Array.prototype.unshift, you might see a performance improvement right away when using big arrays, even if you don't need to insert or delete from the middle of your array.

Installation

npm i @oresoftware/linked-queue

Examples

const {LinkedQueue} from '@oresoftware/linked-queue';
const q = new LinkedQueue();

q.enq({task:'foo'});       // adds item to the end/tail of the queue
q.enqueue({task:'bar'});   // adds item to the end/tail of the queue
q.push({task:'far'});      // adds item to the end/tail of the queue

q.enq('foo', {task:'bbb'}); // adds item to the end/tail of the queue, with id/key = 'foo'
q.get('foo'); // => {key: 'foo', value: {task:'bbb'}}


q.enq({whatever:'is clever'}, {task:'bbb'}); // adds task to the end/tail of the queue, with id/key = {whatever:'is clever'}


q.deq();      // => removes and returns the head of the queue, or null.
q.dequeue();  // => removes and returns the head of the queue, or null.
q.shift();    // => removes and returns the head of the queue, or null.


q.peek(); // => returns the head of the queue, but does not remove it.

More features

Removing/deleting items from the queue in constant time

This is a FIFO queue, but if you wanted to dequeue a item that was not the head of the queue, instead of using q.deq(), you could just use q.remove(id). Where id is the key/id of the item you wish to retrieve. And this would be in constant time. Removing items from the queue is useful if certain items in the queue have been obviated or are stale, etc.

Inserting items into the queue in constant time

Removing from the middle of the queue is an important feature - inserting items into the queue in linear time is also important. You can insert items into the queue in a certain location, in constant time, by using:

q.insertInFrontOf(k1, k2, v);
q.insertBehind(k1, k2, v);

Inserting in non-constant time

q.insertAtIndex(index, k, v);

Further explanation

A simple example of why this works is, with an array, each shift/unshift call is O(N), so the following takes 80 seconds!

const values = [];

const t = Date.now();

for (let i = 0; i < 100000; i++) {
  values.unshift({});
}

console.log('total time:', Date.now() - t); // takes almost 80 seconds!

However if we add to the front of a queue, with this library, it takes just 60ms. More than 2 orders of magnitude difference. Huge.

const {LinkedQueue} = require('@oresoftware/linked-queue');

const q = new LinkedQueue();

const t = Date.now();

for (let i = 0; i < 100000; i++) {
  q.addToFront({});
}

console.log('total time:', Date.now() - t);  // just 60 milliseconds!