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

binary-search-promises

v1.0.3

Published

Asynchronous binary search, using promises.

Downloads

58

Readme

binary-search-promises

Javascript library to do binary search using promises. This allows for asynchronous comparison functions.

Usage

Install

npm install binary-search-promises

Arguments

function binarySearch(haystack, needle, compare, low, high)
  • haystack - A sorted array.
  • needle - The element to find in haystack.
  • compare - A comparison function, with a twist. Instead of returning a result directly, it must return a Promise that will resolve to the comparison result. (The comparison algorithm should be the same as the one used to sort.)
  • low - Optional lower bound of the haystack to consider. You probably don't need to use this.
  • high - Optional upper bound of the haystack to consider. You probably don't need to use this.

compare is optional if dealing with numbers or strings.

If, for some reason, you want to use a synchronous comparison function, you can wrap it with binarySearch.wrapComparator().

Return

The function returns an array of two values:

  • The first value indicates whether the needle was found in the haystack.
  • The second value is the position of needle in haystack, if it was found. If it was not found, then it is the position where it should be inserted to maintain the sorting order.

Example

let binarySearch = require('binary-search-promises');

let haystack = [1,2,3,4,5,6,7,8,9];
let needle = 4;

binarySearch(haystack, needle, function(a, b) {
    let req = new Request(`/secret-comparison-algorithm/${a}/{$b}/`);
    fetch(req).then(function(response) {
        // For this example, the response comes back as one of -1, 0, or 1 as plain text.
        // text() returns a Promise that resolves to that text.
        return response.text();
    }).catch(function(reason) {
        return Promise.reject(reason);
    });
}).then(function(resolution) {
    let [found, position] = resolution;
    if (found) {
        console.log(`${needle} is at ${position}`);
    } else {
        console.log(`${needle} should be inserted at ${position}`);
    }
}).catch(function(reason) {
    console.error(reason);
});

Okay, that's a silly example. You're probably not going to be doing asynchronous comparisons on numbers.