baseDifference.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var baseIndexOf = require('./baseIndexOf'),
  2. cacheIndexOf = require('./cacheIndexOf'),
  3. createCache = require('./createCache');
  4. /** Used as the size to enable large array optimizations. */
  5. var LARGE_ARRAY_SIZE = 200;
  6. /**
  7. * The base implementation of `_.difference` which accepts a single array
  8. * of values to exclude.
  9. *
  10. * @private
  11. * @param {Array} array The array to inspect.
  12. * @param {Array} values The values to exclude.
  13. * @returns {Array} Returns the new array of filtered values.
  14. */
  15. function baseDifference(array, values) {
  16. var length = array ? array.length : 0,
  17. result = [];
  18. if (!length) {
  19. return result;
  20. }
  21. var index = -1,
  22. indexOf = baseIndexOf,
  23. isCommon = true,
  24. cache = (isCommon && values.length >= LARGE_ARRAY_SIZE) ? createCache(values) : null,
  25. valuesLength = values.length;
  26. if (cache) {
  27. indexOf = cacheIndexOf;
  28. isCommon = false;
  29. values = cache;
  30. }
  31. outer:
  32. while (++index < length) {
  33. var value = array[index];
  34. if (isCommon && value === value) {
  35. var valuesIndex = valuesLength;
  36. while (valuesIndex--) {
  37. if (values[valuesIndex] === value) {
  38. continue outer;
  39. }
  40. }
  41. result.push(value);
  42. }
  43. else if (indexOf(values, value, 0) < 0) {
  44. result.push(value);
  45. }
  46. }
  47. return result;
  48. }
  49. module.exports = baseDifference;