# binary-search

> tiny binary search function with comparators

Latest version **1.3.6** (published 2019-08-06) · CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install binary-search
pnpm add binary-search
yarn add binary-search
bun add binary-search
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.6 |
| Published | 2019-08-06 |
| First published | 2013-05-21 |
| Weekly downloads | 0 |
| License | CC0-1.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 88 |
| Author | The Dark Sky Company, LLC |
| Maintainers | darkskybot, ironwallaby, jeffbyrnes |

## Links

- npm: https://www.npmjs.com/package/binary-search
- Repository: https://github.com/darkskyapp/binary-search
- Homepage: https://github.com/darkskyapp/binary-search#readme
- Issues: https://github.com/darkskyapp/binary-search/issues
- npm.io page: https://npm.io/package/binary-search

## Recent versions

- 1.3.6 (latest) — 2019-08-06
- 1.3.5 — 2019-02-08
- 1.3.4 — 2018-06-25
- 1.3.3 — 2018-01-29
- 1.3.2 — 2016-08-04
- 1.3.1 — 2016-08-03
- 1.3.0 — 2016-04-26
- 1.2.0 — 2014-04-14
- 1.1.2 — 2014-04-09
- 1.1.1 — 2013-10-16
- 1.1.0 — 2013-10-10
- 1.0.2 — 2013-08-29
- 1.0.1 — 2013-05-21
- 1.0.0 — 2013-05-21

## README

binary-search
=============

This is a really tiny, stupid, simple binary search library for Node.JS. We
wrote it because existing solutions were bloated and incorrect.

This version is a straight port of the Java version mentioned by Joshua Bloch
in his article, [Nearly All Binary Searches and Merge Sorts are Broken](http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html).

Thanks to [Conrad Irwin](https://github.com/ConradIrwin) and [Michael
Marino](https://github.com/mgmarino) for, ironically, pointing out bugs.

Example
-------

```js
var bs = require("binary-search");

bs([1, 2, 3, 4], 3, function(element, needle) { return element - needle; });
// => 2

bs([1, 2, 4, 5], 3, function(element, needle) { return element - needle; });
// => -3
```

Be advised that passing in a comparator function is *required*. Since you're
probably using one for your sort function anyway, this isn't a big deal.

The comparator takes a 1st and 2nd argument of element and needle, respectively.

The comparator also takes a 3rd and 4th argument, the current index and array,
respectively. You shouldn't normally need the index or array to compare values,
but it's there if you do.

You may also, optionally, specify an input range as the final two parameters,
in case you want to limit the search to a particular range of inputs. However,
be advised that this is generally a bad idea (but sometimes bad ideas are
necessary).

License
-------

To the extent possible by law, The Dark Sky Company, LLC has [waived all
copyright and related or neighboring rights][cc0] to this library.

[cc0]: http://creativecommons.org/publicdomain/zero/1.0/

---
_Source: https://npm.io/package/binary-search · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
