Registry / devops / shuffled-priority-queue

shuffled-priority-queue

JSON →
library2.1.0jsnpmunverified

A priority queue that shuffles elements with the same priority. Current stable version is 2.1.0. The package is lightweight and provides a simple API for adding, removing, shifting, popping, and iterating over items. It is suitable for scenarios where elements of equal priority should be processed in a random order, such as load balancing or fair scheduling. The package is actively maintained and has no external dependencies.

npm install shuffled-priority-queue
INSTALL
IMPORT
SIG · SHUFFLED-PRIORITY-
S
shuffled-priority-queue
devopsjavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const spq = require('shuffled-priority-queue')
import spq from 'shuffled-priority-queue'
The package is primarily CommonJS; ESM import may not work without bundler or in Node.js without experimental modules.
default
const queue = spq()
const queue = new spq()
spq is a factory function, not a class constructor.
default
const queue = require('shuffled-priority-queue')()
const queue = require('shuffled-priority-queue').spq()
The default export is the factory function itself; no named export.

Demonstrates basic usage: creating a queue, adding items with priorities, and shifting items. Items with same priority are shuffled.

const spq = require('shuffled-priority-queue'); const queue = spq(); queue.add({ priority: 0, value: 'hello' }); queue.add({ priority: 0, value: 'world' }); queue.add({ priority: 1, value: 'welt' }); queue.add({ priority: 2, value: 'verden' }); console.log(queue.shift()); // { value: 'verden' } console.log(queue.shift()); // { value: 'welt' } console.log(queue.shift()); // { value: 'hello' } or { value: 'world' } console.log(queue.shift()); // { value: 'hello' } or { value: 'world' } console.log(queue.shift()); // null
Debug
Known issues
gotchaThe `add` method expects an object with a `priority` property. If priority is missing or undefined, the item will be added with priority `undefined` which may cause unexpected ordering.
fix
Always set a numeric `priority` on objects passed to `add()`, or handle items without priority separately.
affects: >=1.0.0
breakingIn version 2.0.0, the package switched from a different internal implementation, and the iteration order for `next` and `prev` may differ from v1. The `head` and `tail` methods were added in v2.
fix
Review code that uses `next` or `prev` iteration if upgrading from v1 to v2.
affects: >=2.0.0 <3.0.0
Errors
Common errors & fixes
TypeError: spq is not a constructor
Trying to use `new spq()` instead of calling `spq()`
fix
Use `const queue = spq()` (no `new` keyword).
Cannot find module 'shuffled-priority-queue'
Package not installed.
fix
Run `npm install shuffled-priority-queue`.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
shuffled-priority-queue — npm install shuffled-priority-queue · libregistry