Détail du package

@datastructures-js/queue

datastructures-js94.3kMIT4.2.3

a performant queue implementation in javascript

queue, queue es6, queue js

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

Queue.dequeueArray.shift
~27 ms~4 mins 31 secs

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

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

[4.2.3] - 2022-09-05

Fixed

  • readme.

[4.2.2] - 2022-07-30

Fixed

  • added types to packge.json
  • readme.

[4.2.1] - 2022-05-30

Fixed

  • added push & pop to ts types.

[4.2.0] - 2022-05-30

Added

  • push & pop as synonyms for enqueue & dequeue

[4.1.4] - 2022-05-15

Fixed

  • README

[4.1.3] - 2021-06-20

Fixed

  • index.d.ts

[4.1.2] - 2021-06-20

Fixed

  • export.

[4.1.1] - 2021-06-14

Fixed

  • README.

[4.1.0] - 2021-06-13

Added

  • typescript.

[4.0.0] - 2021-01-02

Changed

  • .enqueue can now chain other queue methods.
  • named export for queue class.

Fixed

  • readme

[3.1.4] - 2020-12-27

Fixed

  • jsdoc
  • readme

[3.1.3] - 2020-04-03

Fixed

  • .npmignore

[3.1.2] - 2020-04-02

Fixed

  • README

[3.1.1] - 2020-04-02

Fixed

  • README & jsdoc

[3.1.0] - 2019-12-28

Added

  • .clone() to create a shallow copy of the queue.
  • Enable creating the queue from an existing list
    • new Queue(list) enable passing a list in the constructor.
    • Queue.fromArray(list) static function.