fast-deep-equal
The fastest deep equality check with Date, RegExp and ES6 Map, Set and typed arrays support
Top Related Projects
A modern JavaScript utility library delivering modularity, performance, & extras.
JavaScript's utility _ belt
:ram: Practical functional Javascript
Quick Overview
fast-deep-equal is a high-performance JavaScript library for deep equality comparison of objects and arrays. It provides a fast and efficient way to compare complex nested structures, supporting various data types including Date, RegExp, and typed arrays.
Pros
- Extremely fast performance compared to other deep equality libraries
- Supports a wide range of JavaScript data types and structures
- Small footprint with no dependencies
- Works in both Node.js and browser environments
Cons
- Does not support circular references
- May not handle some edge cases or custom object types without additional configuration
- Limited customization options for comparison behavior
Code Examples
Comparing simple objects:
const equal = require('fast-deep-equal');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
const obj3 = { a: 1, b: { c: 3 } };
console.log(equal(obj1, obj2)); // true
console.log(equal(obj1, obj3)); // false
Comparing arrays with nested objects:
const equal = require('fast-deep-equal');
const arr1 = [1, 2, { a: [3, 4] }];
const arr2 = [1, 2, { a: [3, 4] }];
const arr3 = [1, 2, { a: [3, 5] }];
console.log(equal(arr1, arr2)); // true
console.log(equal(arr1, arr3)); // false
Comparing Date objects:
const equal = require('fast-deep-equal');
const date1 = new Date('2023-05-01');
const date2 = new Date('2023-05-01');
const date3 = new Date('2023-05-02');
console.log(equal(date1, date2)); // true
console.log(equal(date1, date3)); // false
Getting Started
To use fast-deep-equal in your project, follow these steps:
-
Install the package using npm:
npm install fast-deep-equal
-
Import the library in your JavaScript file:
const equal = require('fast-deep-equal');
-
Use the
equal
function to compare objects or arrays:const result = equal(object1, object2); console.log(result); // true or false
That's it! You can now use fast-deep-equal to perform efficient deep equality comparisons in your JavaScript projects.
Competitor Comparisons
A modern JavaScript utility library delivering modularity, performance, & extras.
Pros of lodash
- Comprehensive utility library with a wide range of functions
- Well-established and widely used in the JavaScript ecosystem
- Modular structure allows for importing only needed functions
Cons of lodash
- Larger bundle size, which may impact performance in some applications
- More complex API due to its extensive feature set
- Overkill for projects that only need deep equality comparison
Code Comparison
fast-deep-equal:
const equal = require('fast-deep-equal');
const obj1 = { a: [1, 2], b: { c: 3 } };
const obj2 = { a: [1, 2], b: { c: 3 } };
console.log(equal(obj1, obj2)); // true
lodash:
const _ = require('lodash');
const obj1 = { a: [1, 2], b: { c: 3 } };
const obj2 = { a: [1, 2], b: { c: 3 } };
console.log(_.isEqual(obj1, obj2)); // true
Summary
fast-deep-equal is a lightweight, focused library for deep equality comparisons, while lodash is a comprehensive utility library. fast-deep-equal is more suitable for projects that only need deep equality checks and prioritize small bundle size. lodash is better for projects that require a wide range of utility functions and don't mind the additional overhead. The code comparison shows that both libraries can perform deep equality checks with similar syntax, but lodash requires importing the entire library or using a more complex import strategy.
JavaScript's utility _ belt
Pros of Underscore
- Comprehensive utility library with a wide range of functions
- Well-established and widely used in many JavaScript projects
- Provides cross-browser compatibility for many operations
Cons of Underscore
- Larger file size and potential performance overhead
- May include unnecessary functions for projects only needing deep equality checks
- Less specialized for deep equality comparisons
Code Comparison
Underscore:
_.isEqual(obj1, obj2);
Fast-deep-equal:
const equal = require('fast-deep-equal');
equal(obj1, obj2);
Summary
Underscore is a full-featured utility library offering a wide range of functions, including deep equality checks. It's well-established and provides cross-browser compatibility. However, it comes with a larger file size and potential performance overhead.
Fast-deep-equal, on the other hand, is a specialized library focused solely on deep equality comparisons. It's lightweight and optimized for performance in this specific task. While it lacks the breadth of functionality offered by Underscore, it excels in its targeted use case.
For projects requiring only deep equality checks, Fast-deep-equal may be the more efficient choice. However, if a project already uses Underscore or needs its broader set of utilities, using Underscore's _.isEqual
method could be more convenient.
:ram: Practical functional Javascript
Pros of Ramda
- Comprehensive functional programming library with a wide range of utility functions
- Supports function composition and currying for more flexible and reusable code
- Immutable data handling, promoting safer and more predictable code
Cons of Ramda
- Larger bundle size due to its extensive feature set
- Steeper learning curve for developers unfamiliar with functional programming concepts
- May be overkill for projects that only need simple deep equality checks
Code Comparison
Fast-deep-equal:
import equal from 'fast-deep-equal';
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
console.log(equal(obj1, obj2)); // true
Ramda:
import * as R from 'ramda';
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
console.log(R.equals(obj1, obj2)); // true
While both libraries can perform deep equality checks, Fast-deep-equal is focused solely on this task, making it more lightweight and potentially faster for this specific use case. Ramda, on the other hand, offers a broader range of functional programming utilities, making it more versatile for complex data manipulation tasks beyond simple equality checks.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
fast-deep-equal
The fastest deep equal with ES6 Map, Set and Typed arrays support.
Install
npm install fast-deep-equal
Features
- ES5 compatible
- works in node.js (8+) and browsers (IE9+)
- checks equality of Date and RegExp objects by value.
ES6 equal (require('fast-deep-equal/es6')
) also supports:
- Maps
- Sets
- Typed arrays
Usage
var equal = require('fast-deep-equal');
console.log(equal({foo: 'bar'}, {foo: 'bar'})); // true
To support ES6 Maps, Sets and Typed arrays equality use:
var equal = require('fast-deep-equal/es6');
console.log(equal(Int16Array([1, 2]), Int16Array([1, 2]))); // true
To use with React (avoiding the traversal of React elements' _owner property that contains circular references and is not needed when comparing the elements - borrowed from react-fast-compare):
var equal = require('fast-deep-equal/react');
var equal = require('fast-deep-equal/es6/react');
Performance benchmark
Node.js v12.6.0:
fast-deep-equal x 261,950 ops/sec ±0.52% (89 runs sampled)
fast-deep-equal/es6 x 212,991 ops/sec ±0.34% (92 runs sampled)
fast-equals x 230,957 ops/sec ±0.83% (85 runs sampled)
nano-equal x 187,995 ops/sec ±0.53% (88 runs sampled)
shallow-equal-fuzzy x 138,302 ops/sec ±0.49% (90 runs sampled)
underscore.isEqual x 74,423 ops/sec ±0.38% (89 runs sampled)
lodash.isEqual x 36,637 ops/sec ±0.72% (90 runs sampled)
deep-equal x 2,310 ops/sec ±0.37% (90 runs sampled)
deep-eql x 35,312 ops/sec ±0.67% (91 runs sampled)
ramda.equals x 12,054 ops/sec ±0.40% (91 runs sampled)
util.isDeepStrictEqual x 46,440 ops/sec ±0.43% (90 runs sampled)
assert.deepStrictEqual x 456 ops/sec ±0.71% (88 runs sampled)
The fastest is fast-deep-equal
To run benchmark (requires node.js 6+):
npm run benchmark
Please note: this benchmark runs against the available test cases. To choose the most performant library for your application, it is recommended to benchmark against your data and to NOT expect this benchmark to reflect the performance difference in your application.
Enterprise support
fast-deep-equal package is a part of Tidelift enterprise subscription - it provides a centralised commercial support to open-source software users, in addition to the support provided by software maintainers.
Security contact
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerability via GitHub issues.
License
Top Related Projects
A modern JavaScript utility library delivering modularity, performance, & extras.
JavaScript's utility _ belt
:ram: Practical functional Javascript
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot