New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

std-queue

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

std-queue

Efficient fifo queue for handling large amounts of data in O(1)

latest
Source
npmnpm
Version
0.1.6
Version published
Weekly downloads
36
414.29%
Maintainers
1
Weekly downloads
 
Created
Source

std-queue

Efficient fifo queue for handling large amounts of data in O(1)

Description

This module was created because most other queue modules in javascript either use simple arrays or doubly linked lists.

Both have drawbacks: Large arrays are terribly inefficient when removing the first element and linked lists are not very efficent storage-wise.

Usage

import Queue from 'std-queue';

const queue = new Queue();

queue.push(1);
queue.push(2);

for (let i = 10; i < 10000; i++) {
  queue.push(i);
}

queue.push('Hello', 'World');
queue.peek(); // 1
queue.last(); // 'World';
queue.shift(); // 1, head is now at 2
queue.length; // 9993
queue.clear(); // Empties queue
queue.shift(); // undefined

Underlying implementation

The underlying implementation is a simple linked list of subqueues (delayed shift arrays) of maximum size 1000 each.

  • When adding an element, it is pushed to the back of the last subqueue, or a new one if the last subqueue is full.
  • Each subqueue is an array and the index of the element at the front of the subqueue
  • When removing the first element, the index is increased in the first subqueue. If it reaches the end of the subqueue, the subqueue is destroyed. The subqueue is never shifted, so each operation is efficient.

The only operations performed are:

  • pushing an element to the back of an array
  • adding a new subqueue
  • destroying the front subqueue
  • moving an index in a subqueue

Each of those operations are O(1), there is never any array resizing. The use of arrays instead of just linked lists allow to be faster and use less RAM than linked lists.

Contributions

If you have contributions or feature requests, open a pull request or create a new issue. I will glady add basic features if they are needed.

Keywords

queue

FAQs

Package last updated on 03 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts