Skip to content

Conversation

@lionel-rowe
Copy link
Contributor

Towards #3069.

Note that this adds a new binarySearch function under collections, which operates on array-likes of sorted numbers/bigints/strings and implements the same semantics as C#'s Array.BinarySearch and Java's Arrays.binarySearch. It doesn't patch the existing BinarySearchTree under data-structures (@WWRS has a separate open PR for that: #6544).

assertEquals(binarySearch([0, 1], 0), 0)
assertEquals(binarySearch([0, 1], 1), 1)
assertEquals(binarySearch([0, 1], -0.5), -1) // (bitwise complement of 0)
assertEquals(binarySearch([0, 1], 0.5), -2) // (bitwise complement of 1)
assertEquals(binarySearch([0, 1], 1.5), -3) // (bitwise complement of 2)

@codecov
Copy link

codecov bot commented Apr 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.80%. Comparing base (e932f30) to head (0612469).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6628   +/-   ##
=======================================
  Coverage   94.80%   94.80%           
=======================================
  Files         574      575    +1     
  Lines       46511    46530   +19     
  Branches     6549     6553    +4     
=======================================
+ Hits        44093    44112   +19     
  Misses       2375     2375           
  Partials       43       43           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +7 to +10
* Binary searching may be preferable to `Array#findIndex` if the array is
* large and performance is at a premium, or if information about the insertion
* index is needed upon non-exact matches (`Array#findIndex` simply returns
* `-1` in such cases).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kt3k kt3k merged commit 95bf2d8 into denoland:main May 14, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants