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

@toolbuilder/priority-buffer

v0.1.6

Published

Priority queue/buffer with capacity limits and custom comparator. Methods push/shift/length match the Array API.

Downloads

148

Readme

PriorityBuffer

PriorityBuffer implements a simple fixed length priority queue. Your comparator function is used for prioritization. If the buffer is full, lower priority items are dropped as new ones are added. If two items have the same priority, the older one has higher priority than the newer one. The methods push, shift, and length match the Array API to make substitution easier.

PriorityBuffer is a minimal implementation developed for use with Await-For-It iterable queues.

There are two related buffers:

  • RingBuffer ring buffer with fixed maximum size - faster than Array as a buffer.
  • DynamicRingBuffer ring buffer that manages memory in chunks to support large capacity for data bursts with low overhead when small.

Performance

In common use cases, the time to evaluate prioritized items swamps the time required to prioritize them. Therefore, buffer performance isn't likely an issue in normal use. Nevertheless, I tried several packages with higher theoretical performance O(log(n)) for the underlying implementation. Surprisingly, all of them performed substantially worse than the simple O(n/2) implementation selected. I lack an explanation.

The current implementation is very fast if the inserted item is lower or higher in priority than all the currently buffered items. When the item fits somewhere in the buffer, the implementation resorts to O(n/2) insertion.

To measure performance, I wrote a quick and dirty test that grows and shrinks the buffer repeatedly. The roughly 100 million push/shift operations ensure that the insertion performance is emphasized.

  • RingBuffer(no priority ordering): 982.927ms
  • Array (no priority ordering): 2.615s
  • PriorityBuffer: 21.720s ouch

Installation

npm install --save @toolbuilder/priority-buffer

Getting Started

The API documentation is here. This is a quick example to get you started.

import { PriorityBuffer } from '@toolbuilder/priority-buffer'
const log = console.log

const comparator = (a, b) => a.localeCompare(b)
const buffer = new PriorityBuffer(comparator, 10) // max length 10
log(buffer.length) // prints 0

const input = ['C', 'A', 'B'] // unprioritized input
input.forEach(x => buffer.push(x))
log([...buffer]) // prints ['A', 'B', 'C']
log(buffer.length) // prints 3
log(buffer.front()) // prints 'A'
log(buffer.back()) // prints 'C'
log(buffer.shift()) // removes highest priority element. Prints 'A'
log(buffer.length) // prints 2
log([...buffer]) // prints ['B', 'C']

Alternatives

There are lots of alternatives on npm.

Contributing

Contributions are welcome. Please create a pull request.

  • I use pnpm instead of npm.
  • Package verification requires pnpm to be installed globally.
    • npm install -g pnpm
    • pnpm install --frozen-lockfile
    • pnpm test to run unit tests
    • pnpm run check:packfile to test against Node ES and CommonJS projects, as well as Electron.
    • pnpm run check to validate the package is ready for commit
    • pnpm run release to update version, update changelog using Conventional Commits, check it all in, and tag the release.

Issues

This project uses Github issues.

License

MIT