time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

_baseSortedIndex.js (1429B)


      1 var baseSortedIndexBy = require('./_baseSortedIndexBy'),
      2     identity = require('./identity'),
      3     isSymbol = require('./isSymbol');
      4 
      5 /** Used as references for the maximum length and index of an array. */
      6 var MAX_ARRAY_LENGTH = 4294967295,
      7     HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
      8 
      9 /**
     10  * The base implementation of `_.sortedIndex` and `_.sortedLastIndex` which
     11  * performs a binary search of `array` to determine the index at which `value`
     12  * should be inserted into `array` in order to maintain its sort order.
     13  *
     14  * @private
     15  * @param {Array} array The sorted array to inspect.
     16  * @param {*} value The value to evaluate.
     17  * @param {boolean} [retHighest] Specify returning the highest qualified index.
     18  * @returns {number} Returns the index at which `value` should be inserted
     19  *  into `array`.
     20  */
     21 function baseSortedIndex(array, value, retHighest) {
     22   var low = 0,
     23       high = array == null ? low : array.length;
     24 
     25   if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
     26     while (low < high) {
     27       var mid = (low + high) >>> 1,
     28           computed = array[mid];
     29 
     30       if (computed !== null && !isSymbol(computed) &&
     31           (retHighest ? (computed <= value) : (computed < value))) {
     32         low = mid + 1;
     33       } else {
     34         high = mid;
     35       }
     36     }
     37     return high;
     38   }
     39   return baseSortedIndexBy(array, value, identity, retHighest);
     40 }
     41 
     42 module.exports = baseSortedIndex;