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

queue-fifo

v0.2.6

Published

Javascript implementation of a queue data structure

Downloads

29,735

Readme

npm version Build Status dependency status devDependency status npm

Queue Data Structure

Description

This is a javascript implementation of a queue data structure.

A queue data structure models the notion of 'First in First Out', or FIFO— image the line at the grocery store. The first item to be removed from a queue is the first item placed in the queue. Basically, the order in which items are placed in the queue matters.

This particular implementation utilizes a linked list as the underlying data structure. This offers several benefits.

  • We can leverage the work that has already been done to implement the linked list.

  • This lends itself to a level of composition and abstraction which greatly simplifies this implementation. It provides a wrapper around only those methods of the linked list that we need to construct the properties of a queue data structure.

  • The 'queue' or 'dequeue' operations can be completed in O(1) time.

  • No additional overhead is required to 'resize' the data structure to add more elements to the queue. When elements are 'queued' up in the queue, the underlying linked list will adjust its size dynamically.

For specific examples and documentation, see the below sections

Motivation:

The main purpose of this project is revisit the basics, and focus on the development process.

I wholehearedly acknowledge that the basic data structure space is populated with well-written code and efficient implementations, and one could easily grab one of those libraries and integrate it in their project. However, the main difference between those libraries/implementations and this one is that this is the best implementation I have ever written. My hope is that someone else will find this useful, but understand, this code is not the goal; this will simply be a useful bi-product of the journey. The underlying motivation is to understand and, more importantly, learn from the process to get to the desired end-state—for me it is all about the joy of the journey.

Environment:

Although this implementation is designed to be used with Node.js, it could be used in other contexts with minor modifications. This implementation does not have any external dependencies that would preclude it from being used in the browser--just include it with a <script> tag and it should be good to go. Disclaimer: I have not tested this implementation in any other context/environment; only tested with node.js


Basic Usage

Install with npm :

npm install queue-fifo --save

Install with yarn :

yarn add queue-fifo

Basic usage example below. Note: it does not cover all the available methods, rather just highlights the main functionality to get up and running with this data structure. For a description of all the methods, see the API section.

var Queue = require('queue-fifo');
var queue = new Queue();

queue.isEmpty();
// --> true

queue.enqueue('data item 1');
queue.enqueue('data item 2');
queue.enqueue('data item 3');
queue.enqueue('data item 4');
// queue contains:
// 'data item 1', <-- front
//  ... ,
// 'data item 4'

queue.isEmpty();
// --> false

queue.size();
// --> 4

queue.dequeue();
// --> removes 'data item 1'

queue.peek()
// --> 'data item 2'

queue.size();
// --> 3

queue.clear();
queue.isEmpty();
// --> true

TypeScript definitions are also provided:

import Queue = require('queue-fifo');

let stringQueue = new Queue<string>();
let numberQueue = new Queue<number>();

API

Available methods for a queue instance:

  • isEmpty()

    Determines if the queue is empty or not. Returns true if is empty, false otherwise.

  • size()

    Returns the size of the queue, or number of items

  • clear()

    Clears the queue of all data

  • enqueue(data)

    Adds an new item containing 'data' to the back of the queue

  • dequeue()

    Removes the item from the front of the queue

  • peek()

    Returns the data of the item at the front of the queue, but does not remove it


License

MIT © Jason Jones