time-to-botec

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

_baseIntersection.js (2262B)


      1 var SetCache = require('./_SetCache'),
      2     arrayIncludes = require('./_arrayIncludes'),
      3     arrayIncludesWith = require('./_arrayIncludesWith'),
      4     arrayMap = require('./_arrayMap'),
      5     baseUnary = require('./_baseUnary'),
      6     cacheHas = require('./_cacheHas');
      7 
      8 /* Built-in method references for those with the same name as other `lodash` methods. */
      9 var nativeMin = Math.min;
     10 
     11 /**
     12  * The base implementation of methods like `_.intersection`, without support
     13  * for iteratee shorthands, that accepts an array of arrays to inspect.
     14  *
     15  * @private
     16  * @param {Array} arrays The arrays to inspect.
     17  * @param {Function} [iteratee] The iteratee invoked per element.
     18  * @param {Function} [comparator] The comparator invoked per element.
     19  * @returns {Array} Returns the new array of shared values.
     20  */
     21 function baseIntersection(arrays, iteratee, comparator) {
     22   var includes = comparator ? arrayIncludesWith : arrayIncludes,
     23       length = arrays[0].length,
     24       othLength = arrays.length,
     25       othIndex = othLength,
     26       caches = Array(othLength),
     27       maxLength = Infinity,
     28       result = [];
     29 
     30   while (othIndex--) {
     31     var array = arrays[othIndex];
     32     if (othIndex && iteratee) {
     33       array = arrayMap(array, baseUnary(iteratee));
     34     }
     35     maxLength = nativeMin(array.length, maxLength);
     36     caches[othIndex] = !comparator && (iteratee || (length >= 120 && array.length >= 120))
     37       ? new SetCache(othIndex && array)
     38       : undefined;
     39   }
     40   array = arrays[0];
     41 
     42   var index = -1,
     43       seen = caches[0];
     44 
     45   outer:
     46   while (++index < length && result.length < maxLength) {
     47     var value = array[index],
     48         computed = iteratee ? iteratee(value) : value;
     49 
     50     value = (comparator || value !== 0) ? value : 0;
     51     if (!(seen
     52           ? cacheHas(seen, computed)
     53           : includes(result, computed, comparator)
     54         )) {
     55       othIndex = othLength;
     56       while (--othIndex) {
     57         var cache = caches[othIndex];
     58         if (!(cache
     59               ? cacheHas(cache, computed)
     60               : includes(arrays[othIndex], computed, comparator))
     61             ) {
     62           continue outer;
     63         }
     64       }
     65       if (seen) {
     66         seen.push(computed);
     67       }
     68       result.push(value);
     69     }
     70   }
     71   return result;
     72 }
     73 
     74 module.exports = baseIntersection;