Fhernd

baseDifference.js

Mar 6th, 2026
19,268
0
Never
16
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.98 KB | Source Code | 0 0
  1. function baseDifference(array, values, iteratee, comparator) {
  2.   let index = -1;
  3.   let length = array.length;
  4.   let result = [];
  5.  
  6.   let includes = arrayIncludes;
  7.   let isCommon = true;
  8.  
  9.   if (!length) {
  10.     return result;
  11.   }
  12.  
  13.   if (iteratee) {
  14.     values = values.map(iteratee);
  15.   }
  16.  
  17.   if (comparator) {
  18.     includes = arrayIncludesWith;
  19.     isCommon = false;
  20.   } else if (values.length >= 200) {
  21.     includes = cacheHas;
  22.     isCommon = false;
  23.     values = new SetCache(values);
  24.   }
  25.  
  26.   outer:
  27.   while (++index < length) {
  28.     let value = array[index];
  29.     let computed = iteratee == null ? value : iteratee(value);
  30.  
  31.     if (isCommon && computed === computed) {
  32.       let valuesIndex = values.length;
  33.       while (valuesIndex--) {
  34.         if (values[valuesIndex] === computed) {
  35.           continue outer;
  36.         }
  37.       }
  38.       result.push(value);
  39.     } else if (!includes(values, computed, comparator)) {
  40.       result.push(value);
  41.     }
  42.   }
  43.  
  44.   return result;
  45. }
Advertisement