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 🙏

© 2025 – Pkg Stats / Ryan Hefner

generic-binary-search

v0.0.1

Published

A versatile and efficient TypeScript library for performing various binary search operations on arrays of any type.

Readme

Generic Binary Search Library

A versatile and efficient TypeScript library for performing various binary search operations on arrays of any type.

Quick Examples

import { binaryFind, binaryFindClosest, binaryFindGte } from 'generic-binary-search';

const users = [
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Bob' },
  { id: 5, name: 'Charlie' },
  { id: 7, name: 'David' },
];

// Find exact match
const exactMatch = binaryFind(users, 3, (user) => user.id);
console.log(exactMatch); // Output: { id: 3, name: 'Bob' }

// Find closest match
const closestMatch = binaryFindClosest(users, 4, (user) => user.id);
console.log(closestMatch); // Output: { id: 3, name: 'Bob' }

// Find first item greater than or equal to
const gteMatch = binaryFindGte(users, 6, (user) => user.id);
console.log(gteMatch); // Output: { id: 7, name: 'David' }

Features

  • Generic implementation: Works with arrays of any type
  • Customizable value extraction: Provide a function to extract numeric values from your items
  • Multiple search operations:
    • Exact match
    • Closest match
    • Greater than / Less than
    • Greater than or equal / Less than or equal
    • Find all occurrences
    • Find items within a range
    • Find item containing a value (for range-based data)

Rationale

Binary search is a powerful algorithm for quickly finding elements in sorted arrays. However, most implementations are limited to simple arrays of numbers. This library provides a generic approach, allowing you to use binary search on arrays of any type by specifying a function to extract numeric values from your items.

This flexibility makes the library useful for a wide range of applications, from simple number arrays to complex objects with multiple fields.

Installation

npm install generic-binary-search

Usage

Here are examples of how to use each function in the library with simple objects, ordered by most common usage:

1. Basic Binary Search (Exact Match)

import { binaryFind, binaryFindIndex } from 'generic-binary-search';

const users = [
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Bob' },
  { id: 5, name: 'Charlie' },
  { id: 7, name: 'David' },
];

// Find the item with id 3
const user = binaryFind(users, 3, (user) => user.id);
console.log(user); // Output: { id: 3, name: 'Bob' }

// Find the index of the item with id 5
const index = binaryFindIndex(users, 5, (user) => user.id);
console.log(index); // Output: 2

2. Closest Match

import { binaryFindClosest, binaryFindClosestIndex } from 'generic-binary-search';

const users = [
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Bob' },
  { id: 5, name: 'Charlie' },
  { id: 7, name: 'David' },
];

// Find the user with id closest to 4
const closestUser = binaryFindClosest(users, 4, (user) => user.id);
console.log(closestUser); // Output: { id: 3, name: 'Bob' }

// Find the index of the user with id closest to 6
const closestIndex = binaryFindClosestIndex(users, 6, (user) => user.id);
console.log(closestIndex); // Output: 2 (index of Charlie)

3. Greater Than or Equal / Less Than or Equal

import { binaryFindGte, binaryFindLte, binaryFindGteIndex, binaryFindLteIndex } from 'generic-binary-search';

const users = [
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Bob' },
  { id: 5, name: 'Charlie' },
  { id: 7, name: 'David' },
];

// Find the first user with id greater than or equal to 4
const gteUser = binaryFindGte(users, 4, (user) => user.id);
console.log(gteUser); // Output: { id: 5, name: 'Charlie' }

// Find the last user with id less than or equal to 6
const lteUser = binaryFindLte(users, 6, (user) => user.id);
console.log(lteUser); // Output: { id: 5, name: 'Charlie' }

// Find the index of the first user with id greater than or equal to 5
const gteIndex = binaryFindGteIndex(users, 5, (user) => user.id);
console.log(gteIndex); // Output: 2

// Find the index of the last user with id less than or equal to 4
const lteIndex = binaryFindLteIndex(users, 4, (user) => user.id);
console.log(lteIndex); // Output: 1 (index of Bob)

4. Greater Than / Less Than

import { binaryFindGtIndex, binaryFindLtIndex } from 'generic-binary-search';

const users = [
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Bob' },
  { id: 5, name: 'Charlie' },
  { id: 7, name: 'David' },
];

// Find the index of the first user with id greater than 4
const gtIndex = binaryFindGtIndex(users, 4, (user) => user.id);
console.log(gtIndex); // Output: 2 (index of Charlie)

// Find the index of the last user with id less than 6
const ltIndex = binaryFindLtIndex(users, 6, (user) => user.id);
console.log(ltIndex); // Output: 2 (index of Charlie)

5. Find Items Within a Range

import { binaryFindBetween, binaryFindIndicesBetween } from 'generic-binary-search';

const users = [
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Bob' },
  { id: 5, name: 'Charlie' },
  { id: 7, name: 'David' },
];

// Find users with ids between 2 and 6
const rangeUsers = binaryFindBetween(users, 2, 6, (user) => user.id);
console.log(rangeUsers); // Output: [{ id: 3, name: 'Bob' }, { id: 5, name: 'Charlie' }]

// Find indices of users with ids between 2 and 6
const rangeIndices = binaryFindIndicesBetween(users, 2, 6, (user) => user.id);
console.log(rangeIndices); // Output: [1, 2]

6. Find All Occurrences

import { binaryFindAll, binaryFindAllIndices } from 'generic-binary-search';

const users = [
  { id: 1, name: 'Alice' },
  { id: 3, name: 'Bob' },
  { id: 5, name: 'Charlie' },
  { id: 5, name: 'Chris' },
  { id: 7, name: 'David' },
];

// Find all users with id 5
const allUsers = binaryFindAll(users, 5, (user) => user.id);
console.log(allUsers); // Output: [{ id: 5, name: 'Charlie' }, { id: 5, name: 'Chris' }]

// Find all indices of users with id 5
const allIndices = binaryFindAllIndices(users, 5, (user) => user.id);
console.log(allIndices); // Output: [2, 3]

7. Find Item Containing a Value (Range-based data)

import { binaryFindRangeItem } from 'generic-binary-search';

const ranges = [
  { start: 0, end: 10, name: 'Range A' },
  { start: 11, end: 20, name: 'Range B' },
  { start: 21, end: 30, name: 'Range C' },
  { start: 31, end: 40, name: 'Range D' },
];

// Find the range containing 15
const range = binaryFindRangeItem(
  ranges,
  15,
  (item) => item.start,
  (item) => item.end,
);
console.log(range); // Output: { start: 11, end: 20, name: 'Range B' }

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.