<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
var isArray = Array.isArray;
var keyList = Object.keys;
var hasProp = Object.prototype.hasOwnProperty;
function equal(a, b) {
if (a === b) return true;
if (a && b && typeof a == 'object' && typeof b == 'object') {
var arrA = isArray(a)
, arrB = isArray(b)
, i
, length
, key;
if (arrA && arrB) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0;)
if (!equal(a[i], b[i])) return false;
return true;
}
if (arrA != arrB) return false;
var dateA = a instanceof Date
, dateB = b instanceof Date;
if (dateA != dateB) return false;
if (dateA && dateB) return a.getTime() == b.getTime();
var regexpA = a instanceof RegExp
, regexpB = b instanceof RegExp;
if (regexpA != regexpB) return false;
if (regexpA && regexpB) return a.toString() == b.toString();
var keys = keyList(a);
length = keys.length;
if (length !== keyList(b).length)
return false;
for (i = length; i-- !== 0;)
if (!hasProp.call(b, keys[i])) return false;
for (i = length; i-- !== 0;) {
key = keys[i];
if (!equal(a[key], b[key])) return false;
}
return true;
}
return a!==a && b!==b;
};
// 1 level deep
window.foo1 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } };
window.bar1 = { a: 1, b: 3, c: { a: 1, b: 2, c: { a: 1, b: 2 } } };
// 2 levels deep
window.foo2 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } };
window.bar2 = { a: 1, b: 2, c: { a: 1, b: 3, c: { a: 1, b: 2 } } };
// 3 levels deep
window.foo3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } };
window.bar3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 4 } } };
_.isEqual(window.foo1, window.bar1)
equal(window.foo1, window.bar1);
JSON.stringify(window.foo1) === JSON.stringify(window.bar1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
fast-deep-equal | |
JSON |
Test name | Executions per second |
---|---|
lodash | 638070.2 Ops/sec |
fast-deep-equal | 649338.2 Ops/sec |
JSON | 434104.6 Ops/sec |
I'll break down the provided benchmark definition and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark is defined as a JSON object that contains three test cases:
_.isEqual(window.foo1, window.bar1)
: Tests the implementation of the isEqual
function from the Lodash library.equal(window.foo1, window.bar1);
: Tests the custom implementation of the fast-deep-equal
function.JSON.stringify(window.foo1) === JSON.stringify(window.bar1);
: Tests the equality of two JSON strings.Options Compared
The three test cases compare different approaches to deep equality checking:
isEqual
function: A widely used and well-tested library function for deep equality checks.fast-deep-equal
: A custom function that attempts to outperform Lodash's isEqual
function. However, this implementation has a higher chance of introducing bugs or performance issues.Pros and Cons
Here are some pros and cons associated with each approach:
isEqual
function:fast-deep-equal
:isEqual
function in terms of performance.Library and Its Purpose
The Lodash library is a popular JavaScript utility library that provides a set of functions for various tasks, including deep equality checks. In this benchmark, the isEqual
function from Lodash is used to test its implementation.
Special JS Features or Syntax
There are no special JS features or syntax used in these benchmarks. The focus is on testing the performance and correctness of different approaches to deep equality checks.
Other Alternatives
If you're interested in exploring alternative libraries or implementations for deep equality checks, here are a few options:
deepEqual
function similar to Lodash's isEqual
.Keep in mind that these alternatives may have different strengths and weaknesses compared to Lodash's isEqual
function.