Github https://github.com/epoberezkin/fast-deep-equal
https://github.com/epoberezkin/fast-deep-equal
image.png
https://bundlephobia.com/package/fast-deep-equal@3.1.3
lodash.isEqual
image.png

最快的对象深度相等对比,支持

  • Date
  • RegExp
  • ES6 Map、Set
  • 数组
  • 对象 ```jsx yarn add fast-deep-equal

import equal from ‘fast-deep-equal’;

const equal = require(‘fast-deep-equal/react’); const equal = require(‘fast-deep-equal/es6/react’);

  1. <a name="sGLwQ"></a>
  2. ## fastEqual
  3. ```javascript
  4. module.exports = function equal(a, b) {
  5. if (a === b) return true;
  6. if (a && b && typeof a == 'object' && typeof b == 'object') {
  7. if (a.constructor !== b.constructor) return false;
  8. var length, i, keys;
  9. if (Array.isArray(a)) {
  10. length = a.length;
  11. if (length != b.length) return false;
  12. for (i = length; i-- !== 0;)
  13. if (!equal(a[i], b[i])) return false;
  14. return true;
  15. }
  16. if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
  17. if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
  18. if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
  19. keys = Object.keys(a);
  20. length = keys.length;
  21. if (length !== Object.keys(b).length) return false;
  22. for (i = length; i-- !== 0;)
  23. if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
  24. for (i = length; i-- !== 0;) {
  25. var key = keys[i];
  26. if (!equal(a[key], b[key])) return false;
  27. }
  28. return true;
  29. }
  30. // true if both NaN, false otherwise
  31. return a!==a && b!==b;
  32. };

es6

  1. var envHasBigInt64Array = typeof BigInt64Array !== 'undefined';
  2. module.exports = function equal(a, b) {
  3. if (a === b) return true;
  4. if (a && b && typeof a == 'object' && typeof b == 'object') {
  5. if (a.constructor !== b.constructor) return false;
  6. var length, i, keys;
  7. if (Array.isArray(a)) {
  8. length = a.length;
  9. if (length != b.length) return false;
  10. for (i = length; i-- !== 0;)
  11. if (!equal(a[i], b[i])) return false;
  12. return true;
  13. }
  14. if ((a instanceof Map) && (b instanceof Map)) {
  15. if (a.size !== b.size) return false;
  16. for (i of a.entries())
  17. if (!b.has(i[0])) return false;
  18. for (i of a.entries())
  19. if (!equal(i[1], b.get(i[0]))) return false;
  20. return true;
  21. }
  22. if ((a instanceof Set) && (b instanceof Set)) {
  23. if (a.size !== b.size) return false;
  24. for (i of a.entries())
  25. if (!b.has(i[0])) return false;
  26. return true;
  27. }
  28. if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
  29. length = a.length;
  30. if (length != b.length) return false;
  31. for (i = length; i-- !== 0;)
  32. if (a[i] !== b[i]) return false;
  33. return true;
  34. }
  35. if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
  36. if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
  37. if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
  38. keys = Object.keys(a);
  39. length = keys.length;
  40. if (length !== Object.keys(b).length) return false;
  41. for (i = length; i-- !== 0;)
  42. if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
  43. for (i = length; i-- !== 0;) {
  44. var key = keys[i];
  45. if (!equal(a[key], b[key])) return false;
  46. }
  47. return true;
  48. }
  49. // true if both NaN, false otherwise
  50. return a!==a && b!==b;
  51. };