Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including parrot-queue with all npm packages installed. Try it out:

var parrotQueue = require("parrot-queue")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

parrot-queue v1.0.1

An efficient queue in JavaScript.

parrot-queue

An efficient queue in JavaScript. Note that using native arrays as a queue leads to O(n) runtime complexity for dequeuing, because shift takes O(n) in the worst case.

Usage

const ParrotQueue = require("parrot-queue");

const cinemaQueue = ParrotQueue();
cinemaQueue.enqueue('Clara');
cinemaQueue.enqueue('Peter');
cinemaQueue.enqueue('Lisa');
cinemaQueue.enqueue('John');

console.log(cinemaQueue.size()) // "4"

console.log(cinemaQueue.peek()) // "Clara"
console.log(cinemaQueue.dequeue()) // "Clara"
console.log(cinemaQueue.dequeue()) // "Peter"
console.log(cinemaQueue.dequeue()) // "Lisa"
console.log(cinemaQueue.size()) // "1"

Methods

  • enqueue(object: any): void Adds an object to the queue
  • size(): number Returns the size of the queue
  • peek(): any Return most recently added element
  • dequeue(): any Return most recently added element and delete it from the queue

Runtime complexity

  • enqueue: O(1)
  • size: O(1)
  • dequeue: O(1)
  • peek: O(1)
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free