function binarySearch(array, itemToFind, comparator) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("array", array);
  Check.defined("itemToFind", itemToFind);
  Check.defined("comparator", comparator);
  //>>includeEnd('debug');

  let low = 0;
  let high = array.length - 1;
  let i;
  let comparison;

  while (low <= high) {
    i = ~~((low + high) / 2);
    comparison = comparator(array[i], itemToFind);
    if (comparison < 0) {
      low = i + 1;
      continue;
    }
    if (comparison > 0) {
      high = i - 1;
      continue;
    }
    return i;
  }
  return ~(high + 1);
}
Finds an item in a sorted array.

@function
@param {Array} array The sorted array to search.
@param {*} itemToFind The item to find in the array.
@param {binarySearchComparator} comparator The function to use to compare the item to
       elements in the array.
@returns {number} The index of <code>itemToFind</code> in the array, if it exists.  If <code>itemToFind</code>
       does not exist, the return value is a negative number which is the bitwise complement (~)
       of the index before which the itemToFind should be inserted in order to maintain the
       sorted order of the array.

@example
// Create a comparator function to search through an array of numbers.
function comparator(a, b) {
    return a - b;
};
const numbers = [0, 2, 4, 6, 8];
const index = Cesium.binarySearch(numbers, 6, comparator); // 3