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

@datastructures-js/queue

v4.2.3

Published

a performant queue implementation in javascript

Downloads

81,290

Readme

@datastructures-js/queue

npm npm npm

A performant queue implementation in javascript.

Contents

Install

npm install --save @datastructures-js/queue

require

const { Queue } = require('@datastructures-js/queue');

import

import { Queue } from '@datastructures-js/queue';

API

constructor

JS
// empty queue
const queue = new Queue();

// from an array
const queue = new Queue([1, 2, 3]);
TS
// empty queue
const queue = new Queue<number>();

// from an array
const queue = new Queue<number>([1, 2, 3]);

Queue.fromArray

JS
// empty queue
const queue = Queue.fromArray([]);

// with elements
const list = [10, 3, 8, 40, 1];
const queue = Queue.fromArray(list);

// If the list should not be mutated, use a copy of it.
const queue = Queue.fromArray(list.slice());
TS
// empty queue
const queue = Queue.fromArray<number>([]);

// with elements
const list = [10, 3, 8, 40, 1];
const queue = Queue.fromArray<number>(list);

enqueue (push)

adds an element to the back of the queue.

queue.enqueue(10).enqueue(20); // or queue.push(123)

front

peeks on the front element of the queue.

console.log(queue.front()); // 10

back

peeks on the back element in the queue.

console.log(queue.back()); // 20

dequeue (pop)

removes and returns the front element of the queue in O(1) runtime.

console.log(queue.dequeue()); // 10 // or queue.pop()
console.log(queue.front()); // 20

Dequeuing all elements takes O(n) instead of O(n2) when using shift/unshift with arrays.

Explanation by @alexypdu:

Internally, when half the elements have been dequeued, we will resize the dynamic array using Array.slice() which runs in $O(n)$. Since dequeuing all $n$ elements will resize the array $\log_2n$ times, the complexity is $$1 + 2 + 4 + \cdots + 2^{\log_2 n - 1} = 2 ^ {(\log_2 n - 1) + 1} - 1 = n - 1 = O(n)$$ Hence the overall complexity of dequeuing all elements is $O(n + n) = O(n)$, and the amortized complexity of dequeue() is thus $O(1)$.

benchmark:

dequeuing 1 million elements in Node v14

isEmpty

checks if the queue is empty.

console.log(queue.isEmpty()); // false

size

returns the number of elements in the queue.

console.log(queue.size()); // 1

clone

creates a shallow copy of the queue.

const queue = Queue.fromArray([{ id: 2 }, { id: 4 } , { id: 8 }]);
const clone =  queue.clone();

clone.dequeue();

console.log(queue.front()); // { id: 2 }
console.log(clone.front()); // { id: 4 }

toArray

returns a copy of the remaining elements as an array.

queue.enqueue(4).enqueue(2);
console.log(queue.toArray()); // [20, 4, 2]

clear

clears all elements from the queue.

queue.clear();
queue.size(); // 0

Build

grunt build

License

The MIT License. Full License is here