Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
bsearch
Advanced tools
Changelog
[1.0.0-1] - (2022-01-23)
Readme
Utility functions for performing binary search in various scenarios (sync/async, ints/arrays/floats).
Use case | Sync | Async |
---|---|---|
Integers | smallestInt largestInt | smallestIntAsync largestIntAsync |
Floats | smallestFloat largestFloat | smallestFloatAsync largestFloatAsync |
Array indices | firstIndex lastIndex | firstIndexAsync lastIndexAsync |
Array elements | firstElement lastElement | firstElementAsync lastElementAsync |
https://apiref.page/package/bsearch
Suppose you have an array of chapters in a book.
const book = [
{ page: 0, chapter: 'Front Cover' },
{ page: 2, chapter: 'Preamble' },
{ page: 5, chapter: 'Table of Contents' },
{ page: 8, chapter: 'Chapter 1' },
{ page: 29, chapter: 'Chapter 2' },
{ page: 48, chapter: 'Chapter 3' },
]
To answer the question: “What chapter am I on if I’m on page 20?”
With that, the following code finds the answer:
import * as bsearch from 'bsearch'
const chapter = bsearch.lastElement(book, (chapter) => 20 >= chapter.page)
Suppose you have an array of numbers.
const numbers = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
You want to insert a new number n
into the array while maintaining the sorted order. There are two ways:
Finding the first possible insertion index:
const indexToInsertBefore = bsearch.firstIndex(numbers, (x) => n <= x)
const insertionIndex =
indexToInsertBefore === -1 ? numbers.length : indexToInsertBefore
Finding the last possible insertion index:
const indexToInsertAfter = bsearch.lastIndex(numbers, (x) => n >= x)
const insertionIndex = indexToInsertBefore + 1
Suppose you want to draw some text in a canvas. You want to find out what is the largest possible font size that will fit the text within a given width.
const fontSize = bsearch.largestInt(1, 1000, (fontSize) => {
ctx.font = `${fontSize}px sans-serif`
return ctx.measureText(text).width <= availableWidth
})
FAQs
Binary Search for JavaScript
The npm package bsearch receives a total of 8 weekly downloads. As such, bsearch popularity was classified as not popular.
We found that bsearch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.