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 🙏

© 2025 – Pkg Stats / Ryan Hefner

elegant-queue

v1.0.8

Published

To address this inefficiency when processing large amounts of data, designing a queue with O(1) time complexity can solve many problems.

Readme

elegant-queue

Overview

In JavaScript and TypeScript, arrays are often used to implement queues. The built-in shift() method removes the element at the zeroth index and shifts the remaining elements down, which has O(n) time complexity due to the re-indexing required.

Why Circular Buffers?

To optimize queue operations, especially with large datasets, a circular buffer is a highly effective solution. It allows both enqueue and dequeue operations to be performed in O(1) time complexity by managing elements in a fixed-size array with wrapping pointers.

Key Benefits:

  • Memory Efficiency: A circular buffer uses a fixed-size array and wraps around, eliminating the need for continuous resizing and minimizing memory overhead.
  • Consistent O(1) Performance: Operations remain constant time, avoiding the performance pitfalls of array resizing and shifting.
  • Avoids Memory Fragmentation: Efficient memory use and reduced risk of fragmentation, even with dynamic queue sizes.

📚 Getting Started

elegant-queue supports both CommonJS and ES Modules.

CommonJS

const { Queue } = require('elegant-queue');

ES Modules

import { Queue } from 'elegant-queue';

🔎 Explore features

constructor(arraySize: number)

arraySize: number (optional): This parameter defines the size of each internal array block within the linked nodes of the queue. It specifies how many elements each node in the queue can hold. By default, this value is set to 4096 if no argument is provided.

enqueue(value: T)

This method adds a new element to the end of the queue.

dequeue()

This method removes and returns the element at the front of the queue. If the queue is empty, it throws a EmptyQueueException.

peek()

This method returns the element at the front of the queue without removing it. If the queue is empty, it throws a EmptyQueueException.

clear()

This method clears all elements from the queue.(effectively resetting the queue to its initial state.)

size()

This method returns the number of elements currently in the queue.

isEmpty()

This method checks if the queue is empty. It returns true if _head is equal to _tail (indicating no elements are present), and false otherwise.

🌈 Examples

Basic Usage Examples

import { Queue } from 'elegant-queue';

// 1. Create an empty queue with default buffer size (4096)
const queue1 = new Queue<number>();
queue1.enqueue(1);
queue1.enqueue(2);
console.log(queue1.dequeue()); // 1

// 2. Create an empty queue with custom buffer size
const queue2 = new Queue<string>(1024); // Buffer size of 1024
queue2.enqueue('hello');
queue2.enqueue('world');
console.log(queue2.peek()); // 'hello'

// 3. Initialize queue with data (using default buffer size)
const queue3 = new Queue([1, 2, 3, 4, 5]);
queue3.enqueue(6); // [1, 2, 3, 4, 5, 6]
const item = queue3.dequeue();
console.log(item); // 1
console.log(queue3.toArray()); // [2, 3, 4, 5, 6]

// 4. Initialize queue with data and custom buffer size
const queue4 = new Queue(['a', 'b', 'c'], 512); // Data + buffer size of 512
console.log(queue4.size()); // 3

Advanced Usage Examples

import { Queue } from 'elegant-queue';

// Working with objects
interface Task {
  id: number;
  name: string;
  priority: number;
}

const taskQueue = new Queue<Task>();
taskQueue.enqueue({ id: 1, name: 'Process data', priority: 1 });
taskQueue.enqueue({ id: 2, name: 'Send email', priority: 2 });

// Iterating through queue without removing elements
for (const task of taskQueue) {
  console.log(`Task: ${task.name}, Priority: ${task.priority}`);
}

// Converting to array for processing
const allTasks = taskQueue.toArray();
console.log(`Total tasks: ${allTasks.length}`);

// Clear all tasks
taskQueue.clear();
console.log(taskQueue.isEmpty()); // true

Exception Handling Example

import { Queue, EmptyQueueException } from 'elegant-queue';

try {
  const item = queue.dequeue(); // Attempt to remove the item from the queue.
  console.log('Dequeued item:', item);
} catch (error) {
  if (error instanceof EmptyQueueException) {
    console.error('Queue is empty. Cannot dequeue an item.');
  } else {
    console.error('An unexpected error occurred:', error);
  }
}

⚡️ Performance (1 million numbers)

The following benchmarks compare elegant-queue with a standard array-based queue.

Array Queue performance:

console.time('ArrayQueue Enqueue Time');
const arrayQueue: Array<number> = [];

for (let i = 0; i < LARGE_DATA_SIZE; i++) {
  arrayQueue.push(i);
}
console.timeEnd('ArrayQueue Enqueue Time');

console.time('ArrayQueue Dequeue Time');
while (arrayQueue.length > 0) {
  arrayQueue.shift();
}
console.timeEnd('ArrayQueue Dequeue Time');

Array Queue performance result:

  console.time
    ArrayQueue Dequeue Time: 109907 ms

Elegant Queue performance:

console.time('ElegantQueue Enqueue Time');
const elegantQueue = new Queue<number>();

for (let i = 0; i < LARGE_DATA_SIZE; i++) {
  elegantQueue.enqueue(i);
}
console.timeEnd('ElegantQueue Enqueue Time');

console.time('ElegantQueue Dequeue Time');
while (elegantQueue.size() > 0) {
  elegantQueue.dequeue();
}
console.timeEnd('ElegantQueue Dequeue Time');

Elegant Queue performance result:

  console.time
    ElegantQueue Dequeue Time: 5 ms

Note: The shift() method in arrays has O(n) time complexity due to the need to re-index elements after removal. In contrast, elegant-queue provides O(1) time complexity for both enqueue and dequeue operations by utilizing a circular buffer design, making it significantly faster for large datasets.

⚠️ Performance Considerations & Alternatives

There is No Silver Bullet

While elegant-queue provides excellent performance for many use cases, there is no silver bullet in software engineering. Before adopting this library in production, consider the following:

Evaluate Alternatives

Before settling on elegant-queue, thoroughly research and evaluate other queue implementations:

  • Native JavaScript solutions: For simple use cases, native arrays might be sufficient
  • Other npm packages: Libraries like denque, double-ended-queue, or yallist may better suit your specific needs
  • Built-in data structures: Modern JavaScript engines are highly optimized; simple solutions often perform surprisingly well

Buffer Size Optimization

The default buffer size of 4096 is optimized for high-throughput scenarios, but it may not be optimal for your specific use case:

Benchmark Different Buffer Sizes

// Test different buffer sizes for your specific use case
const smallQueue = new Queue<YourDataType>(256); // Memory-efficient
const mediumQueue = new Queue<YourDataType>(1024); // Balanced approach
const largeQueue = new Queue<YourDataType>(4096); // High-performance (default)
const xlQueue = new Queue<YourDataType>(8192); // Maximum throughput

// Benchmark with your actual data and usage patterns
console.time('Small Buffer Performance');
// ... your test code
console.timeEnd('Small Buffer Performance');

Buffer Size Guidelines

  • Small buffers (64-512): Better for memory-constrained environments, small queues
  • Medium buffers (1024-2048): Good balance for most applications
  • Large buffers (4096+): Optimal for high-throughput, batch processing scenarios

When to Use Smaller/Larger Buffers

Consider smaller buffers when:

  • Memory usage is a critical concern
  • Queue typically holds fewer than 1000 items
  • Running in memory-constrained environments (mobile, IoT)

Consider larger buffers when:

  • Processing millions of items
  • Memory is abundant
  • Maximum throughput is the primary concern

Performance Testing Recommendations

  1. Benchmark with real data: Test with your actual data types and sizes
  2. Test realistic scenarios: Use patterns that match your production workload
  3. Measure memory usage: Monitor both allocated memory and garbage collection pressure
  4. Compare alternatives: Always benchmark against other solutions
  5. Profile in production-like environments: Development and production performance can differ significantly

Remember: measure, don't assume. The best solution depends entirely on your specific use case, data patterns, and performance requirements.