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

bucket-priority-queue

v2.1.0

Published

Implementation of the bucket queue data structure in TypeScript

Downloads

20

Readme

Bucket Queue in TypeScript

npm Test Status codecov

Overview

BucketQueue is a priority queue implementation ideal for algorithms requiring efficient, priority-based item management, such as Dijkstra's algorithm. It's designed to offer quick enqueue and dequeue operations, particularly when the priority key space is small and comprised of positive integers, a common scenario in many programming puzzles. See Bucket queue on Wikipedia for more.

Benchmarks

Compared to other Heap-based priority queue implementations, BucketQueue can offer a significant performance boost given the right conditions. The following benchmarks - about pop and push operations - were run on a 2021 MacBook Pro with Apple M1 Max chip, Node.js 20.10.0:

BucketQueue x 49,750,443 ops/sec ±0.14% (101 runs sampled)
HeapJS x 12,203,990 ops/sec ±0.18% (99 runs sampled)
HeapDS x 1,094,289 ops/sec ±0.09% (97 runs sampled)
Heap x 4,401,016 ops/sec ±0.10% (98 runs sampled)
MinPriorityQueue x 25,171,646 ops/sec ±0.09% (95 runs sampled)
PriorityQueue x 15,892,132 ops/sec ±0.07% (96 runs sampled)

For more details, see the benchmark source code.

Installation

To install BucketQueue, simply include it in your project dependencies:

npm install bucket-priority-queue

Usage

Here's how you can integrate BucketQueue into your project:

import { MinBucketQueue, MaxBucketQueue } from "bucket-priority-queue";

// Initialize the queue with optional initial items
const queue = new MinBucketQueue<number>([
  [1, 1],
  [2, 2],
  [3, 3],
]);

// Adding items with priority
queue.push(5, 1); // item 5 with priority 1
queue.push(6, 2); // item 6 with priority 2

// Retrieve items with the highest or lowest priority
const lowest = queue.pop(); // returns item with lowest priority

API Reference

MinBucketQueue

MinBucketQueue is a data structure that operates similarly to a priority queue. It organizes elements in a way such that the element with the minimum priority is always at the front.

Methods

  • constructor(items?: [T, Priority][]): Initialize a new MinBucketQueue with optional initial items.
  • push(item: T, priority: Priority): Adds an item to the queue with an associated priority.
  • add(item: T, priority: Priority): Alias for push.
  • offer(item: T, priority: Priority): Alias for push.
  • pop(): T | undefined: Removes and returns the item with the minimum priority. Returns undefined if the queue is empty.
  • poll(): T | undefined: Alias for pop.
  • peek(): T | undefined: Returns the item with the minimum priority without removing it from the queue.
  • clear(): Removes all items from the queue.
  • refill(items: [T, Priority][]): Clears the queue and adds the provided items.
  • has(item: T): Checks if the queue contains the specified item.
  • contains(item: T): Alias for has.
  • toArray(): T[]: Returns an array containing all the items in the queue.
  • isEmpty(): boolean: Returns true if the queue is empty, otherwise returns false.
  • size: number: Contains the number of items in the queue.
  • length: number: Contains the number of items in the queue.

MaxBucketQueue

MaxBucketQueue is a data structure that operates similarly to a priority queue. It organizes elements in a way such that the element with the maximum priority is always at the front.

Methods

  • constructor(items?: [T, Priority][]): Initialize a new MaxBucketQueue with optional initial items.
  • push(item: T, priority: Priority): Adds an item to the queue with an associated priority.
  • add(item: T, priority: Priority): Alias for push.
  • offer(item: T, priority: Priority): Alias for push.
  • pop(): T | undefined: Removes and returns the item with the maximum priority. Returns undefined if the queue is empty.
  • poll(): T | undefined: Alias for pop.
  • peek(): T | undefined: Returns the item with the maximum priority without removing it from the queue.
  • clear(): Removes all items from the queue.
  • refill(items: [T, Priority][]): Clears the queue and adds the provided items.
  • has(item: T): Checks if the queue contains the specified item.
  • contains(item: T): Alias for has.
  • toArray(): T[]: Returns an array containing all the items in the queue.
  • isEmpty(): boolean: Returns true if the queue is empty, otherwise returns false.
  • size: number: Contains the number of items in the queue.
  • length: number: Contains the number of items in the queue.

License

This project is licensed under the MIT License - see the LICENSE file for details.