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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kmccullough/binary-search

v1.0.2

Published

Performs a binary search by iterable or callback

Readme

binary-search

Performs a binary search by iterable or callback

Installation

$ npm install @kmccullough/binary-search

Usage

const LEFT = 0;
const RIGHT = 100;

const search = binarySearch(LEFT, RIGHT).startLow();
for (let [ value, index ] of search) {
  search.isLeft ? search.right() : search.left();
}
console.log(search.value, search.index);

// Or if you prefer a callback...

const result = binarySearch(LEFT, RIGHT).startLow()
  .search((value, index, control) => search.isLeft ? 1 : -1);
console.log(result);

// Log: 0, 100, 50, 25, 13, 6, 3, 2, 1

binarySearch( left: number, right: number, config: BinarySearchConfig ): BinarySearchIterator

Call binarySearch to create and configure a BinarySearchIterator for iteration.

BinarySearchConfig

Configuration may be passed to the binarySearch factory or configured through various chained methods.

binarySearchConfig = {
  startLeft:    null, // True to start with leftmost value (default: startNext)
  startRight:   null, // True to start with rightmost value (default: startNext)
  ratio:        null, // Ratio used to choose next value (default: .5)
  getNextValue: null, // Function callback to return next value
  nextValue:    null, // Next value to use when called for
  epsilon:      null, // Epsilon for float comparisons
  float:        null, // True to force no rounding of values
}

BinarySearchIterator

[Symbol.iterator]()

search( function( value: number, index: number, search: BinarySearchIterator ): number ): number | undefined

BinarySearchIterator implements the Iterable interface and can be used in a for loop. Alternatively, the search function is provided to instead provide a comparator function which will return -1, 1, or 0 to direct the search left, right, or to end the search.

getNextValue( function( left: number, right: number, index: number, search: BinarySearchIterator ): number ): BinarySearchIterator

splitRatio( ratio number ): BinarySearchIterator

ratio: number

By default, the next value is generated by applying the split ratio (default .5) between left and right bounds. If a different ratio is desired, it may be changed by calling the splitRatio method. For more complicated algorithms that decide which value to use next, passing getNextValue a callback function will replace the default implementation. getNextValueByHalf is also exported by the library in case you need the original. The ratio property is available to access the current ratio.

integer(): BinarySearchIterator

float(): BinarySearchIterator

By default, search is limited to integers. Call float to allow for deeper decimal search, but don't forget to end the search.

epsilon( value: number ): BinarySearchIterator

For searches through decimal numbers with float method, the default implementation is to compare the current value against bounds within Number.EPSILON * 10. Call epsilon to set a different value for epsilon for comparing floats.

startNext(): BinarySearchIterator

startLeft(): BinarySearchIterator

startRight(): BinarySearchIterator

startLow(): BinarySearchIterator

startHigh(): BinarySearchIterator

By default, search starts at the next value (i.e. startNext) between bounds, and only then tests the bound towards the desired direction. Call startLeft to start iteration at the left and right bounds, startRight to start iteration at the right and left bounds, and startLow/startHigh to start at the corresponding bounds based on order.

done(): BinarySearchIterator

Call done during iteration to end iteration. 0 may also be returned from comparator search callback to end iteration.

left(): BinarySearchIterator

right(): BinarySearchIterator

lower(): BinarySearchIterator

higher(): BinarySearchIterator

Call left/right or lower/higher during iteration to direct next iterated value towards the desired bound.

nextValue( value: number ): BinarySearchIterator

Call nextValue during or before iteration to set specific next iteration value. Note that when setting the next value prior to iteration, this next value will not be used until a next value is called for; for example, if startLeft is also called prior to iteration, then the left and right bounds would be tested first, before this next value is used.

promote(): BinarySearchIterator

best: number | undefined

Call promote during iteration to mark a value as the current "best". Access best property during or after iteration to access the last promoted value. Passing true for the best argument of the search method makes search return this best value from the search method, rather than the last value.

clone(): BinarySearchIterator

Clones the iterable for searching with the same configuration. Note that each call to \[Symbol.iterator] or search will reset iteration, so running concurrent searches with the same iterable is acceptable to do without cloning.

index: number

Current iteration index during iteration, or last iterated index after iteration.

value: number

Current iteration value during iteration, or last iterated value after iteration. Note that this value is rounded to integer if not searching decimals with float.

floatValue: number

Current iteration value during iteration, or last iterated value after iteration. Note that this value is the non-rounded value if searching decimals with float.

leftBound: number

rightBound: number

lowBound: number

highBound: number

Current iteration bounds.

isLeft: number

isRight: number

isLow: number

isHigh: number

True if current iteration value is equal to a bound.

positiveDirection: number

-1 if left bound is greater than right, otherwise 1

License

MIT