JS ALGORITHMSChapter 8 · JS Algorithms
Binary Search
indexOf and includes are linear searches: they check items one by one, O(n). Binary search needs a sorted array but is O(log n): look at the middle, throw away the half that cannot hold the target, repeat. A million sorted items need about 20 checks instead of a million.
Worked example
How it reads
lowandhighbound the part of the array that could still hold the targetMath.floorkeepsmida whole index- 200 items take at most 8 steps

Cloud tip: Binary search is not only for arrays: you can binary search any yes-or-no question that flips once, like "is this version broken?".


