tinyqueue

repository·main·Indexed 19 days ago

https://github.com/mourner/tinyqueue

A lightweight, minimal binary heap priority queue implementation in JavaScript. It supports min-heap operations by default, custom comparator functions for complex object sorting, and in-place heapification of existing arrays. Core API includes push, pop, and peek methods.

Tokens
999
Snippets
6
Records
6
Agent score
17%

What's inside tinyqueue

  1. Install tinyqueue via npm or CDN

    main

    You can install tinyqueue using npm for Node.js environments or use a CDN for direct browser usage via ESM modules.

    NPM Installation:

    npm install tinyqueue

    Browser Usage (ESM):

    <script type="module">
    	import TinyQueue from 'https://cdn.jsdelivr.net/npm/tinyqueue/+esm';
    </script>
    import TinyQueue from 'tinyqueue';
  2. Use the TinyQueue API

    main

    TinyQueue is a minimal binary heap priority queue. By default, it implements a min-heap (the smallest value is popped first). You can initialize it with an empty constructor, an existing array (which will be modified in-place), or a custom comparator function for non-numeric or complex object sorting.

    Core Methods

    • push(item): Adds an item to the queue.
    • pop(): Removes and returns the top item (the smallest according to the comparator).
    • peek(): Returns the top item without removing it.
    • length: A property representing the number of items in the queue.

    Custom Comparators

    To handle objects or custom sorting logic, pass a comparator function as the second argument to the constructor. The comparator should follow the standard (a, b) => number pattern (returning a negative value if a < b, zero if a === b, and a positive value if a > b).

    // Basic usage with numbers
    let queue = new TinyQueue();
    queue.push(7);
    queue.push(5);
    queue.push(10);
    
    let top = queue.pop(); // returns 5
    top = queue.peek();   // returns 7
    console.log(queue.length); // returns 2
    
    // Initialize from an existing array (modifies the array)
    let arr = [7, 5, 10];
    let queueFromArray = new TinyQueue(arr);
    
    // Usage with custom objects and a comparator
    let objectQueue = new TinyQueue([{value: 5}, {value: 7}], function (a, b) {
    	return a.value - b.value;
    });
    
    // Converting a queue to a sorted array
    const sortedArray = [];
    while (objectQueue.length) sortedArray.push(objectQueue.pop());
  3. Initialize a TinyQueue

    main

    Create a new TinyQueue instance by passing an initial array of data and an optional comparison function. If no comparison function is provided, it defaults to a standard ascending order comparison (a, b) => (a < b ? -1 : a > b ? 1 : 0).

    If an initial array is provided, the queue is automatically heapified to maintain the priority structure.

    import TinyQueue from 'tinyqueue';
    
    // Default min-priority queue
    const queue = new TinyQueue();
    
    // Initialized with data and a custom comparator
    const queueWithData = new TinyQueue([5, 2, 8], (a, b) => a - b);
  4. Pop the highest priority item from TinyQueue

    main

    Remove and return the item at the top of the queue (the element that would be considered 'smallest' by the comparator). If the queue is empty, it returns undefined.

    const item = queue.pop();