What is dequal?
The dequal npm package is a library for performing deep equality checks. It is a lightweight and fast utility for comparing objects, arrays, and other nested data structures to determine if they are equivalent in value.
What are dequal's main functionalities?
Deep equality check for objects
This feature allows you to compare two objects deeply to check if they are equal in value, regardless of whether they are different instances.
const { dequal } = require('dequal');
let obj1 = { a: 1, b: { c: 2 } };
let obj2 = { a: 1, b: { c: 2 } };
console.log(dequal(obj1, obj2)); // true
Deep equality check for arrays
This feature allows you to compare two arrays deeply to check if they are equal in value, including nested arrays.
const { dequal } = require('dequal');
let arr1 = [1, [2, 3]];
let arr2 = [1, [2, 3]];
console.log(dequal(arr1, arr2)); // true
Deep equality check for mixed structures
This feature allows you to compare mixed data structures like objects containing arrays or arrays containing objects to check for deep equality.
const { dequal } = require('dequal');
let mix1 = { a: [1, { b: 2 }] };
let mix2 = { a: [1, { b: 2 }] };
console.log(dequal(mix1, mix2)); // true
Other packages similar to dequal
lodash.isequal
Lodash's isEqual method is a popular utility for performing deep equality checks. It is part of the larger Lodash library, which provides a wide range of utilities for working with JavaScript data types. Compared to dequal, lodash.isequal is part of a larger bundle and may not be as lightweight if you only need the deep equality functionality.
deep-equal
The deep-equal package is another library that provides deep comparison functionality. It is similar to dequal in that it focuses on deep equality checks, but it may have different performance characteristics and API design.
fast-deep-equal
fast-deep-equal is a package that claims to be the fastest deep equality checker. It is similar to dequal in its purpose but may have different implementation details that could affect performance and compatibility in various use cases.
dequal
A tiny (305B) utility to check for deep equality
This module supports comparison of all types, including Function
, RegExp
, Date
, null
, undefined
, and NaN
values. Objects and Arrays are traversed recursively.
Please note that key order within Objects does not matter.
However, the value order within Arrays does matter.
This module exposes three module definitions:
- CommonJS:
dist/dequal.js
- ESModule:
dist/dequal.mjs
- UMD:
dist/dequal.min.js
Install
$ npm install --save dequal
Usage
import dequal from 'dequal';
dequal(1, 1);
dequal({}, {});
dequal('foo', 'foo');
dequal([1, 2, 3], [1, 2, 3]);
dequal(dequal, dequal);
dequal(/foo/, /foo/);
dequal(null, null);
dequal(NaN, NaN);
dequal([], []);
dequal(
[{ a:1 }, [{ b:{ c:[1] } }]],
[{ a:1 }, [{ b:{ c:[1] } }]]
);
dequal(1, '1');
dequal(null, undefined);
dequal({ a:1, b:[2,3] }, { a:1, b:[2,5] });
dequal(/foo/i, /bar/g);
API
dequal(foo, bar)
Returns: Boolean
Both foo
and bar
can be of any type.
A Boolean
is returned indicating if the two were deeply equal.
Benchmarks
Running Node v10.13.0
Load times:
assert: 0.166ms
util: 0.032ms
fast-deep-equal 0.712ms
lodash/isequal 24.700ms
nano-equal 0.506ms
dequal 0.365ms
Validation:
✔ assert.deepStrictEqual
✔ util.isDeepStrictEqual
✔ fast-deep-equal
✔ lodash.isEqual
✔ nano-equal
✔ dequal
Benchmark:
assert.deepStrictEqual x 211,987 ops/sec ±1.47% (92 runs sampled)
util.isDeepStrictEqual x 213,516 ops/sec ±1.25% (93 runs sampled)
fast-deep-equal x 479,017 ops/sec ±1.15% (89 runs sampled)
lodash.isEqual x 207,120 ops/sec ±0.47% (96 runs sampled)
nano-equal x 409,286 ops/sec ±0.73% (97 runs sampled)
dequal x 541,815 ops/sec ±0.95% (94 runs sampled)
License
MIT © Luke Edwards