Deep equality comparison function for JavaScript values
npx @tessl/cli install tessl/npm-lodash--isequal@3.0.0Deep equality comparison function for JavaScript values. Performs comprehensive comparisons between two values to determine if they are equivalent, supporting arrays, booleans, Date objects, numbers, Object objects, regexes, and strings.
npm install lodash.isequalconst isEqual = require('lodash.isequal');For ES modules (if supported by your environment):
import isEqual from 'lodash.isequal';const isEqual = require('lodash.isequal');
// Basic object comparison
const object = { 'user': 'fred' };
const other = { 'user': 'fred' };
object == other;
// => false
isEqual(object, other);
// => true
// Array comparison
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
isEqual(arr1, arr2);
// => true
// Deep nested comparison
const complex1 = {
users: [{ name: 'alice', data: { active: true } }],
count: 1
};
const complex2 = {
users: [{ name: 'alice', data: { active: true } }],
count: 1
};
isEqual(complex1, complex2);
// => truePerforms a deep comparison between two values to determine if they are equivalent.
/**
* Performs a deep comparison between two values to determine if they are
* equivalent. Supports circular reference detection and custom comparison logic.
* If customizer is provided it's invoked to compare values. If customizer returns
* undefined comparisons are handled by the method instead. The customizer is bound
* to thisArg and invoked with up to three arguments: (value, other [, index|key]).
*
* @param {*} value The value to compare.
* @param {*} other The other value to compare.
* @param {Function} [customizer] The function to customize value comparisons.
* @param {*} [thisArg] The this binding of customizer.
* @returns {boolean} Returns true if the values are equivalent, else false.
*/
function isEqual(value, other, customizer, thisArg);Supported Types:
Special Behaviors:
NaN values are considered equal to each other+0 and -0 (they are not equal)__wrapped__ propertyNot Supported (without custom comparator):
Usage Examples:
const isEqual = require('lodash.isequal');
// Different data types
isEqual(1, 1); // => true
isEqual('hello', 'hello'); // => true
isEqual(true, true); // => true
isEqual([1, 2], [1, 2]); // => true
isEqual({ a: 1 }, { a: 1 }); // => true
// Date objects
const date1 = new Date('2023-01-01');
const date2 = new Date('2023-01-01');
isEqual(date1, date2); // => true
// Regular expressions
isEqual(/abc/g, /abc/g); // => true
// Special number handling
isEqual(NaN, NaN); // => true (special case)
isEqual(+0, -0); // => false (distinguishes zero signs)
// Error objects
const err1 = new Error('test');
const err2 = new Error('test');
isEqual(err1, err2); // => true (compared by name and message)
// Custom comparator
const array = ['hello', 'goodbye'];
const other = ['hi', 'goodbye'];
isEqual(array, other, function(value, other) {
if (RegExp.prototype.test.call(/^h(?:i|ello)$/, value) &&
RegExp.prototype.test.call(/^h(?:i|ello)$/, other)) {
return true;
}
});
// => trueThe optional customizer function allows extending comparison support for additional value types and custom comparison logic.
Customizer Function Signature:
value - The value from the first object being comparedother - The value from the second object being comparedindex|key (optional) - The index (for arrays) or key (for objects) being comparedboolean - For definitive custom comparison resultundefined - To defer to default lodash comparison logicthisArg if provided (uses thisArg as the this context)Example with custom comparator:
function customEqual(a, b) {
return isEqual(a, b, function(objValue, othValue) {
// Custom logic for comparing special objects
if (objValue && objValue.isSpecialType) {
return objValue.id === othValue.id;
}
// Return undefined to use default comparison
});
}
const obj1 = { isSpecialType: true, id: 'abc', data: 'different' };
const obj2 = { isSpecialType: true, id: 'abc', data: 'values' };
customEqual(obj1, obj2); // => true (compared by id only)The isEqual function automatically detects and handles circular references in objects and arrays to prevent infinite recursion:
const isEqual = require('lodash.isequal');
// Create objects with circular references
const obj1 = { name: 'test' };
obj1.self = obj1;
const obj2 = { name: 'test' };
obj2.self = obj2;
isEqual(obj1, obj2); // => true
// Works with arrays too
const arr1 = [1, 2];
arr1[2] = arr1;
const arr2 = [1, 2];
arr2[2] = arr2;
isEqual(arr1, arr2); // => true
// Mixed circular references
const complex1 = { data: { users: [] } };
complex1.data.users.push(complex1);
const complex2 = { data: { users: [] } };
complex2.data.users.push(complex2);
isEqual(complex1, complex2); // => trueThe isEqual function itself does not throw errors and will return false for comparisons that cannot be performed. However, custom comparator functions may throw errors if they encounter unexpected input:
const isEqual = require('lodash.isequal');
// Function handles type mismatches gracefully
isEqual(null, undefined); // => false
isEqual({}, null); // => false
isEqual(1, "1"); // => false
// Custom comparator errors are not caught
try {
isEqual(obj1, obj2, function() {
throw new Error('Custom error');
});
} catch (error) {
console.log('Caught:', error.message); // => 'Caught: Custom error'
}