lib/third_party/tracing/base/sorted_array_utils.js
function findClosestIntervalInSortedIntervals(ary, mapLoFn, mapHiFn, val,
                                                maxDiff) {
    if (ary.length === 0)
      return null;

    var idx = findLowIndexInSortedArray(ary, mapLoFn, val);
    if (idx > 0)
      idx -= 1;

    var hiInt = ary[idx];
    var loInt = hiInt;

    if (val > mapHiFn(hiInt) && idx + 1 < ary.length)
      loInt = ary[idx + 1];

    var loDiff = Math.abs(val - mapLoFn(loInt));
    var hiDiff = Math.abs(val - mapHiFn(hiInt));

    if (loDiff > maxDiff && hiDiff > maxDiff)
      return null;

    if (loDiff < hiDiff)
      return loInt;
    else
      return hiInt;
  }
function findClosestIntervalInSortedIntervals(ary, mapLoFn, mapHiFn, val,
                                                maxDiff) {
    if (ary.length === 0)
      return null;

    var idx = findLowIndexInSortedArray(ary, mapLoFn, val);
    if (idx > 0)
      idx -= 1;

    var hiInt = ary[idx];
    var loInt = hiInt;

    if (val > mapHiFn(hiInt) && idx + 1 < ary.length)
      loInt = ary[idx + 1];

    var loDiff = Math.abs(val - mapLoFn(loInt));
    var hiDiff = Math.abs(val - mapHiFn(hiInt));

    if (loDiff > maxDiff && hiDiff > maxDiff)
      return null;

    if (loDiff < hiDiff)
      return loInt;
    else
      return hiInt;
  }
function findClosestIntervalInSortedIntervals ( ary , mapLoFn , mapHiFn , val , maxDiff ) { if ( ary . length === 0 ) return null ; var idx = findLowIndexInSortedArray ( ary , mapLoFn , val ) ; if ( idx > 0 ) idx -= 1 ; var hiInt = ary [ idx ] ; var loInt = hiInt ; if ( val > mapHiFn ( hiInt ) && idx + 1 < ary . length ) loInt = ary [ idx + 1 ] ; var loDiff = Math . abs ( val - mapLoFn ( loInt ) ) ; var hiDiff = Math . abs ( val - mapHiFn ( hiInt ) ) ; if ( loDiff > maxDiff && hiDiff > maxDiff ) return null ; if ( loDiff < hiDiff ) return loInt ; else return hiInt ; }
Finds the closest interval in the implicit array of intervals
defined by ary, mapLoFn and mapHiFn.

This function uses the same scheme as findLowIndexInSortedArray
to define the intervals. The same restrictions on sortedness and
non-overlappingness apply.

@param {Array} ary An array of objects that can be converted into sorted
nonoverlapping ranges [x,y) using the mapLoFn and mapHiFn.
@param {function():*} mapLoFn Callback that produces the low value for the
interval represented by an element in the array.
@param {function():*} mapHiFn Callback that produces the high for the
interval represented by an element in the array.
@param {number} val The value for the search.
@param {number} maxDiff Maximum allowed difference in value between |val|
and an interval's low or high value.
@return {interval} Interval in the array whose high or low value is closest
to |val|, or null if no interval is within range.
Finds the closest interval in the implicit array of intervals defined by ary mapLoFn and mapHiFn .