What is deep-equal?
The deep-equal npm package is a library for performing deep equality checks between two values. It is useful for comparing objects, arrays, and other nested structures to determine if they are equivalent in structure and content, regardless of whether they are the same reference in memory.
What are deep-equal's main functionalities?
Deep comparison of objects and arrays
This feature allows you to compare objects and arrays to see if they are structurally identical, even if they are different instances.
const deepEqual = require('deep-equal');
const obj1 = { a: 1, b: { c: 1 } };
const obj2 = { a: 1, b: { c: 1 } };
console.log(deepEqual(obj1, obj2)); // true
Comparison with custom options
This feature allows you to specify options for the comparison, such as strict mode, which requires the values to be of the same type as well.
const deepEqual = require('deep-equal');
const obj1 = { a: 1 };
const obj2 = { a: '1' };
const options = { strict: true };
console.log(deepEqual(obj1, obj2, options)); // false
Other packages similar to deep-equal
lodash.isequal
Lodash provides a method called isEqual which can perform deep comparisons between two values. It is part of the larger Lodash utility library, which offers a wide range of functions for manipulating and comparing data.
fast-deep-equal
This package offers a fast deep equality comparison algorithm. It is known for its performance and is a good choice when speed is a critical factor.
deep-equal
Node's assert.deepEqual() algorithm
as a standalone module.
This module is around 46 times faster than wrapping assert.deepEqual()
in a try/catch
.
example
var equal = require('deep-equal');
console.dir([
equal(
{ a : [ 2, 3 ], b : [ 4 ] },
{ a : [ 2, 3 ], b : [ 4 ] }
),
equal(
{ x : 5, y : [6] },
{ x : 5, y : 6 }
)
]);
methods
var deepEqual = require('deep-equal')
deepEqual(a, b, opts)
Compare objects a
and b
, returning whether they are equal according to a
recursive equality algorithm.
If opts.strict
is true
, use strict equality (===
) to compare leaf nodes.
The default is to use coercive equality (==
) because that's how
assert.deepEqual()
works by default.
install
With npm do:
npm install deep-equal
test
With npm do:
npm test