function binarySearch(haystack, needle, comparator) {
  let mid, cmp;
  comparator = comparator || ascending;
  let low = 0;
  let high = haystack.length;
  let found = false;

  while (low < high) {
    /* Note that "(low + high) >>> 1" may overflow, and results in a typecast
     * to double (which gives the wrong results). */
    mid = low + ((high - low) >> 1);
    cmp = +comparator(haystack[mid], needle);

    if (cmp < 0.0) {
      /* Too low. */
      low = mid + 1;
    } else {
      /* Key found or too high */
      high = mid;
      found = !cmp;
    }
  }

  /* Key not found. */
  return found ? low : ~low;
}
Performs a binary search on the provided sorted list and returns the index of the item if found. If it can't be found it'll return -1.
https://github.com/darkskyapp/binary-search

@param {Array<*>} haystack Items to search through.
@param {*} needle The item to look for.
@param {Function} [comparator] Comparator function.
@return {number} The index of the item if found, -1 if not.