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

arr-search-binary

v1.0.0

Published

Efficient binary search utilities for sorted arrays, including variations like first & last occurrence, element count, and floor value search.

Readme

arr-search-binary

Efficient binary search utilities for sorted arrays, including variations like first & last occurrence, element count, and floor value search.

Installation

Install the package via npm:

npm i arr-search-binary

Usage

const asb = require('arr-search-binary');

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(asb.binarySearch(arr, 9));  // Output: 8

// OR use it like this:
console.log(asb.binarySearch([1, 2, 3, 4, 5], 3));  // Output: 2

API Reference

| Function | Description | Parameters | Returns | Example | |-----------------------------------|------------------------------------------------------------------|-------------------------------------------------|----------------------------------------------|---------------------------------------------------| | binarySearch(arr, target) | Standard binary search to find an element in a sorted array. | arr (sorted array), target (number to find) | Index of target or -1 if not found. | binarySearch([1, 2, 3, 4, 5], 3) → 2 | | firstOccurrence(arr, target) | Finds the first occurrence of target in a sorted array. | arr (sorted array), target (number to find) | Index of first occurrence or -1 if not found.| firstOccurrence([1, 2, 2, 3], 2) → 1 | | lastOccurrence(arr, target) | Finds the last occurrence of target in a sorted array. | arr (sorted array), target (number to find) | Index of last occurrence or -1 if not found. | lastOccurrence([1, 2, 2, 3], 2) → 2 | | countOccurrences(arr, target) | Counts occurrences of target in a sorted array. | arr (sorted array), target (number to count) | Number of times target appears. | countOccurrences([1, 2, 2, 3], 2) → 2 | | floorValue(arr, target) | Finds the largest element ≤ target. | arr (sorted array), target (number to find floor of) | Floor value or null if none exists. | floorValue([1, 3, 5, 7], 6) → 5 | | ceilValue(arr, target) | Finds the smallest element ≥ target. | arr (sorted array), target (number to find ceil of) | Ceil value or null if none exists. | ceilValue([1, 3, 5, 7], 6) → 7 | | searchInRotatedSortedArray(arr, target) | Finds target in a rotated sorted array. | arr (rotated sorted array), target (number to find) | Index of target or -1 if not found. | searchInRotatedSortedArray([4, 5, 6, 7, 0, 1, 2], 0) → 4 | | findRotationIndex(arr) | Finds the index where a sorted array was rotated. | arr (rotated sorted array) | Rotation index. | findRotationIndex([4, 5, 6, 7, 0, 1, 2]) → 4 | | minInRotatedSortedArray(arr) | Finds the minimum element in a rotated sorted array. | arr (rotated sorted array) | Minimum element. | minInRotatedSortedArray([4, 5, 6, 7, 0, 1, 2]) → 0| | maxInRotatedSortedArray(arr) | Finds the maximum element in a rotated sorted array. | arr (rotated sorted array) | Maximum element. | maxInRotatedSortedArray([4, 5, 6, 7, 0, 1, 2]) → 7| | findPeakElement(arr) | Finds a peak element where an element is greater than its neighbors. | arr (array) | A peak element. | findPeakElement([1, 3, 4, 3, 2]) → 4 | | findMountainPeak(arr) | Finds the peak in a mountain (bitonic) array. | arr (mountain array) | Index of the peak. | findMountainPeak([1, 3, 5, 4, 2]) → 2 | | searchInMountainArray(arr, target)| Searches for target in a mountain array. | arr (mountain array), target (number to find) | Index of target or -1 if not found. | searchInMountainArray([1, 3, 5, 4, 2], 4) → 3 | | squareRoot(n, precision) | Finds the square root of n using binary search. | n (number), precision (decimal places, default: 6) | Approximate square root. | squareRoot(9, 6) → 3.000000 | | nthRoot(n, k, precision) | Finds the kth root of n using binary search. | n (number), k (root degree), precision (decimal places, default: 6) | Approximate kth root. | nthRoot(27, 3, 6) → 3.000000 | | minInSortedRotatedDuplicates(arr) | Finds the minimum in a sorted, rotated array with duplicates. | arr (rotated sorted array with duplicates) | Minimum element. | minInSortedRotatedDuplicates([3, 3, 1, 3]) → 1 |