{"id":14978,"library":"tinyqueue","title":"TinyQueue: Smallest JavaScript Priority Queue","description":"tinyqueue is a lightweight, efficient JavaScript library providing a binary heap-based priority queue data structure. Currently at version 3.0.0, it offers fundamental operations like `push`, `pop`, and `peek` with minimal overhead. The library's core design prioritizes simplicity and a small bundle size, distinguishing it from more feature-rich or specialized queue implementations. It supports custom comparison functions, allowing users to define priority based on object properties rather than just raw values. While release cadence is not strictly regular, it sees updates for performance improvements and compatibility, with the most recent major update (v3.0.0) shifting to an ES module-only distribution.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/mourner/tinyqueue","tags":["javascript","queue","priority","binary heap","data structures","typescript"],"install":[{"cmd":"npm install tinyqueue","lang":"bash","label":"npm"},{"cmd":"yarn add tinyqueue","lang":"bash","label":"yarn"},{"cmd":"pnpm add tinyqueue","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v3.0.0, tinyqueue is published exclusively as an ES module. CommonJS `require()` is no longer supported and will result in an error in modern Node.js environments. TypeScript types are bundled and automatically available for this default export.","wrong":"const TinyQueue = require('tinyqueue');","symbol":"TinyQueue","correct":"import TinyQueue from 'tinyqueue';"}],"quickstart":{"code":"import TinyQueue from 'tinyqueue';\n\n// Create an empty priority queue\nconst queue = new TinyQueue();\n\n// Add some items\nqueue.push(7);\nqueue.push(5);\nqueue.push(10);\n\nconsole.log('Initial top item:', queue.peek()); // Expected: 5\n\n// Remove the top item\nconst top = queue.pop(); // Returns 5\nconsole.log('Popped item:', top);\nconsole.log('Current top item:', queue.peek()); // Expected: 7\nconsole.log('Queue length:', queue.length); // Expected: 2\n\n// Create a priority queue from an existing array\nconst initialArrayQueue = new TinyQueue([7, 5, 10]);\nconsole.log('Queue from array, top:', initialArrayQueue.peek()); // Expected: 5\n\n// Pass a custom item comparator for objects\nconst customQueue = new TinyQueue([{value: 5}, {value: 7}, {value: 3}], function (a, b) {\n    return a.value - b.value;\n});\nconsole.log('Custom queue top item value:', customQueue.peek().value); // Expected: 3\n\n// Turn a queue into a sorted array by repeatedly popping\nconst sortedArray = [];\nwhile (customQueue.length) {\n    sortedArray.push(customQueue.pop().value);\n}\nconsole.log('Sorted array from queue:', sortedArray); // Expected: [3, 5, 7]","lang":"javascript","description":"Demonstrates how to initialize a TinyQueue, add elements, retrieve the highest priority item, inspect the queue's length, and use custom comparators for complex objects, including how to turn a queue into a sorted array."},"warnings":[{"fix":"Migrate your project to use ES module imports (e.g., `import TinyQueue from 'tinyqueue';`). Ensure your Node.js environment or build tool supports ES modules. For browser usage without a build step, load via a CDN that provides ESM compatibility or bundle it with a tool like Rollup/Webpack.","message":"Version 3.0.0 removed CommonJS and UMD distribution support. The package is now exclusively published as an ES module.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Evaluate `flatqueue` if `tinyqueue`'s general-purpose binary heap performance does not meet specific, high-frequency numerical processing requirements.","message":"For use cases strictly involving numerical priority queues where extreme performance is critical, the `flatqueue` library (from the same author) offers a potentially faster, specialized alternative.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you need to preserve the original array, pass a shallow copy to the constructor: `new TinyQueue([...myArray], comparator)`.","message":"When initializing a `TinyQueue` from an existing array, the input array is mutated (rearranged internally) by the queue's constructor to create the heap. This means the original array reference will point to a modified array.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change the import statement to `import TinyQueue from 'tinyqueue';`. Ensure your project's `package.json` correctly specifies `\"type\": \"module\"` if you are using Node.js ES modules, or adjust your build system accordingly.","cause":"Attempting to import `tinyqueue` using CommonJS `require()` syntax (e.g., `const TinyQueue = require('tinyqueue');`) in an ES module context or Node.js environment configured for ES modules (e.g., `\"type\": \"module\"` in package.json) since v3.0.0.","error":"TypeError: require is not a function"},{"fix":"For browser usage without a build system, use a modern CDN that provides ES module builds (e.g., `https://cdn.jsdelivr.net/npm/tinyqueue@3/tinyqueue.mjs`). If using a build system, import `tinyqueue` as an ES module and let your bundler handle it.","cause":"Attempting to directly reference UMD or CJS browser bundles, which were removed in v3.0.0. This error typically occurs when trying to load the library directly in a browser via a script tag referencing a local file or an outdated CDN path.","error":"Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './tinyqueue.min.js' is not defined by 'exports' in .../node_modules/tinyqueue/package.json"}],"ecosystem":"npm"}