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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@hardlydifficult/queue

v1.1.1

Published

A high-performance priority queue with O(1) enqueue and dequeue operations, supporting FIFO ordering within priority levels.

Readme

@hardlydifficult/queue

A high-performance priority queue with O(1) enqueue and dequeue operations, supporting FIFO ordering within priority levels.

Installation

npm install @hardlydifficult/queue

Quick Start

import { createPriorityQueue } from "@hardlydifficult/queue";

const queue = createPriorityQueue<string>();

// Enqueue items with priorities
queue.enqueue("urgent task", "high");
queue.enqueue("normal task", "medium");
queue.enqueue("background task", "low");

// Dequeue in priority order (high → medium → low)
console.log(queue.dequeue()?.data); // "urgent task"
console.log(queue.dequeue()?.data); // "normal task"
console.log(queue.dequeue()?.data); // "background task"

Core Operations

Enqueue and Dequeue

Add items to the queue with a priority level (defaults to "medium"). Items are dequeued in priority order, with FIFO ordering within the same priority level.

const queue = createPriorityQueue<string>();

// Enqueue with explicit priority
const item1 = queue.enqueue("task A", "high");
const item2 = queue.enqueue("task B", "high");
const item3 = queue.enqueue("task C", "medium");

// Dequeue returns highest priority first, FIFO within same priority
queue.dequeue()?.data; // "task A" (high, enqueued first)
queue.dequeue()?.data; // "task B" (high, enqueued second)
queue.dequeue()?.data; // "task C" (medium)

Peek

Inspect the next item without removing it from the queue.

const queue = createPriorityQueue<string>();
queue.enqueue("first", "high");
queue.enqueue("second", "low");

queue.peek()?.data; // "first" (highest priority)
queue.size; // Still 2

Queue Status

Check the number of items and whether the queue is empty.

const queue = createPriorityQueue<string>();

queue.isEmpty; // true
queue.size; // 0

queue.enqueue("item");
queue.isEmpty; // false
queue.size; // 1

Item Management

Remove Items

Remove a specific item by its ID. Returns true if the item was found and removed.

const queue = createPriorityQueue<string>();
const item = queue.enqueue("task", "high");

queue.remove(item.id); // true
queue.size; // 0

Update Priority

Change an item's priority level while keeping its data and enqueue timestamp intact.

const queue = createPriorityQueue<string>();
const item = queue.enqueue("task", "low");

queue.updatePriority(item.id, "high"); // true
queue.peek()?.data; // "task" (now highest priority)

Reorder Within Priority

Move an item before another item in the same priority bucket, or move it to the end of its bucket.

const queue = createPriorityQueue<string>();
const a = queue.enqueue("A", "high");
const b = queue.enqueue("B", "high");
const c = queue.enqueue("C", "high");

// Move A before C
queue.moveBefore(a.id, c.id); // true
queue.toArray().map(i => i.data); // ["B", "A", "C"]

// Move B to the end
queue.moveToEnd(b.id); // true
queue.toArray().map(i => i.data); // ["A", "C", "B"]

Snapshots and Listeners

Get All Items

Retrieve all items in dequeue order without modifying the queue.

const queue = createPriorityQueue<string>();
queue.enqueue("low", "low");
queue.enqueue("high", "high");
queue.enqueue("medium", "medium");

const items = queue.toArray();
items.map(i => i.data); // ["high", "medium", "low"]
queue.size; // Still 3

Listen for Enqueues

Register a callback that fires whenever an item is enqueued. Returns an unsubscribe function.

const queue = createPriorityQueue<string>();

const unsubscribe = queue.onEnqueue((item) => {
  console.log(`Enqueued: ${item.data} (priority: ${item.priority})`);
});

queue.enqueue("task", "high");
// Logs: "Enqueued: task (priority: high)"

unsubscribe();
queue.enqueue("another"); // No log

Clear the Queue

Remove all items from the queue.

const queue = createPriorityQueue<string>();
queue.enqueue("a");
queue.enqueue("b");

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

Item Metadata

Each enqueued item includes metadata accessible via the returned QueueItem object:

const queue = createPriorityQueue<string>();
const item = queue.enqueue("my task", "high");

item.data; // "my task"
item.priority; // "high"
item.id; // "q_1" (unique identifier)
item.enqueuedAt; // 1699564800000 (epoch milliseconds)

Priority Levels

The queue supports three priority levels, dequeued in this order:

| Priority | Dequeue Order | |----------|---------------| | "high" | 1st | | "medium" | 2nd | | "low" | 3rd |

When no priority is specified, "medium" is used as the default.