Coder Social home page Coder Social logo

queue's Introduction

yocto-queue

Tiny queue data structure

You should use this package instead of an array if you do a lot of Array#push() and Array#shift() on large arrays, since Array#shift() has linear time complexity O(n) while Queue#dequeue() has constant time complexity O(1). That makes a huge difference for large arrays.

A queue is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue. A queue works based on the first-in, first-out (FIFO) principle.

Install

$ npm install yocto-queue

Usage

import Queue from 'yocto-queue';

const queue = new Queue();

queue.enqueue('πŸ¦„');
queue.enqueue('🌈');

console.log(queue.size);
//=> 2

console.log(...queue);
//=> 'πŸ¦„ 🌈'

console.log(queue.dequeue());
//=> 'πŸ¦„'

console.log(queue.dequeue());
//=> '🌈'

API

queue = new Queue()

The instance is an Iterable, which means you can iterate over the queue front to back with a β€œfor…of” loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow.

.enqueue(value)

Add a value to the queue.

.dequeue()

Remove the next value in the queue.

Returns the removed value or undefined if the queue is empty.

.clear()

Clear the queue.

.size

The size of the queue.

Related

  • quick-lru - Simple β€œLeast Recently Used” (LRU) cache

queue's People

Contributors

richienb avatar sindresorhus avatar summer-like-coding avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.