Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
priorityqueuejs
Advanced tools
The priorityqueuejs npm package provides a simple and efficient implementation of a priority queue data structure in JavaScript. It allows you to manage a collection of elements where each element is associated with a priority, and elements are dequeued in order of their priority.
Basic Priority Queue Operations
This feature demonstrates the basic operations of a priority queue, including enqueuing elements with different priorities and dequeuing them in order of their priority.
const PriorityQueue = require('priorityqueuejs');
const pq = new PriorityQueue((a, b) => b - a);
pq.enq(5);
pq.enq(1);
pq.enq(3);
console.log(pq.deq()); // Outputs: 5
console.log(pq.deq()); // Outputs: 3
console.log(pq.deq()); // Outputs: 1
Peek at the Highest Priority Element
This feature allows you to peek at the highest priority element without removing it from the queue.
const PriorityQueue = require('priorityqueuejs');
const pq = new PriorityQueue((a, b) => b - a);
pq.enq(5);
pq.enq(1);
pq.enq(3);
console.log(pq.peek()); // Outputs: 5
Check if the Queue is Empty
This feature allows you to check if the priority queue is empty.
const PriorityQueue = require('priorityqueuejs');
const pq = new PriorityQueue((a, b) => b - a);
console.log(pq.isEmpty()); // Outputs: true
pq.enq(5);
console.log(pq.isEmpty()); // Outputs: false
The js-priority-queue package provides a similar priority queue implementation with additional features like custom comparators and support for different queue strategies. It is more flexible in terms of configuration compared to priorityqueuejs.
The heap package offers a binary heap implementation that can be used to create a priority queue. It provides efficient operations for insertion, deletion, and access to the minimum or maximum element, similar to priorityqueuejs.
The tinyqueue package is a minimal and efficient priority queue implementation. It is designed to be lightweight and fast, making it suitable for performance-critical applications. It offers similar functionality to priorityqueuejs but with a smaller footprint.
A simple priority queue data structure for Node.js.
$ npm install priorityqueuejs
var PriorityQueue = require('priorityqueuejs');
var queue = new PriorityQueue(function(a, b) {
return a.cash - b.cash;
});
queue.enq({ cash: 250, name: 'Valentina' });
queue.enq({ cash: 300, name: 'Jano' });
queue.enq({ cash: 150, name: 'Fran' });
queue.size(); // 3
queue.peek(); // { cash: 300, name: 'Jano' }
queue.deq(); // { cash: 300, name: 'Jano' }
queue.size(); // 2
PriorityQueue()
Initializes a new empty PriorityQueue
wich uses .DEFAULT_COMPARATOR()
as
the comparator function for its elements.
PriorityQueue(comparator)
Initializes a new empty PriorityQueue
with uses the given comparator(a, b)
function as the comparator for its elements.
The comparator function must return a positive number when a > b
, 0 when
a == b
and a negative number when a < b
.
PriorityQueue.DEFAULT_COMPARATOR(a, b)
Compares two Number
or String
objects.
PriorityQueue#deq()
Dequeues the top element of the priority queue.
Throws an Error
when the queue is empty.
PriorityQueue#enq(element)
Enqueues the element
at the priority queue and returns its new size.
PriorityQueue#forEach(fn)
Executes fn
on each element. Just be careful to not modify the priorities,
since the queue won't reorder itself.
PriorityQueue#isEmpty()
Returns whether the priority queue is empty or not.
PriorityQueue#peek()
Peeks at the top element of the priority queue.
Throws an Error
when the queue is empty.
PriorityQueue#size()
Returns the size of the priority queue.
$ npm install
$ npm test
MIT
FAQs
a simple priority queue data structure
The npm package priorityqueuejs receives a total of 395,259 weekly downloads. As such, priorityqueuejs popularity was classified as popular.
We found that priorityqueuejs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.