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

k-select-stream

v1.1.0

Published

An efficient online k-min selection algorithm based on min-max heaps.

Downloads

10

Readme

k-select-stream

This package implements on on-line k-min selection algorithm based on an underlying min-max heap. The heap code is a stripped-down version of that used in priority-deque. The package has a single non-default export { KSelect }. KSelect instances implement the generator protocol, so new values can be pushed in and the latest k-min value extracted by calling the next(value) method. KSelect instances are infinite iterables which will repeatedly yield the most up-to-date k-minimum value even if no other values have been pushed since the last peek. Processing each set of n new elements from a data stream requires O(n log_2(k)) time. As k is a constant per object instance, this is effectively constant time per stream element.

API

  • new KSelect<T>(k: number, compare?: (a: T, b: T) => number) Constructs a new KSelect. By default, numbers will be compared numerically, and all other objects will be compared by converting to strings and calling String.localeCompare().
  • clone(): KSelect<T> Creates a shallow copy of the KSelect which remembers the k-smallest elements seen so far by the parent instance in O(k) time.
  • clear() Resets the selector's memory.
  • readonly length: number Indicates how many total items are currently stored in the selector's memory.
  • kmin(): T | undefined Retrieves the k-smallest element seen so far, or undefined if fewer than k elements have yet been seen (i.e., if length is less than k).
  • min(): T | undefined Retrieves the absolute smallest element seen so far, or undefined if no elements have yet been seen (i.e., if length is 0).
  • get(): T[] Retrieves the full set of up-to-k elements stored in the selector's memory, with no guaranteed order.
  • sorted(): T[] Retrieves the full set of up-to-k elements stored in the selector's memory in sorted order.
  • next(value?: T): IteratorResult<T | undefined> Optionally processes a new stream value and returns an iterator result object with the k-smallest element seen so far. If fewer than k elements have been seen so far (i.e., length is less than k), the IteratorResult's value will be undefined.
  • [Symbol.iterator](): IterableIterator<T | undefined> Returns this, which acts as an infinite iterator over the k-smallest elements seen at each step. This is only suitable for use with for(... of ...) loops as long as there is an internal break condition and/or the selector's state is modified inside the loop, as the loop will otherwise run forever yielding the same value repeatedly. KSelect instances should not be used with the spread operator.

Array-Like Methods

  • push(...elements: T[]) Processes new stream elements to update the k-min selection.
  • contains(e: T): boolean Determines whether or not the memory of k-smallest elements seen so far contains a specific element, via === comparison.
  • some(fn: (e: T) => boolean): boolean Determines whether or not any of the k-smallest elements seen so far satisfies the given predicate.
  • every(fn: (e: T) => boolean): boolean Determines whether or not all of the k-smallest elements seen so far satisfy the given predicate.
  • find(fn: (e: T) => boolean): T | undefined Returns an element in the set of k-smallest elements seen so far which satisfies the given predicate, or undefined if there is no such element.
  • forEach(fn: (e: T) => void) Executes the given callback function once for each of the k-smallest elements seen so far; no specific ordering is guaranteed.