<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
// 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,2], b: 2 } } };
window.bar2 = { a: 1, b: 2, c: { a: 1, b: 3, c: { a: [1,2], b: 2 } } };
// 3 levels deep
window.foo3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: [1,2], b: [1,2] } } };
window.bar3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: [1,2], b: [1,2] } } };
_.isEqual(window.foo1, window.bar1)
const areObjectKeysValuesEqual = (obj1, obj2) => {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (key of Object.keys(obj1)) {
if (typeof obj1[key] === "object") {
if (!areObjectKeysValuesEqual(obj1[key], obj2[key])) {
return false;
}
}
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true;
};
const result = areObjectKeysValuesEqual(window.foo1, window.bar1);
_.isEqual(window.foo2, window.bar2)
const areObjectKeysValuesEqual = (obj1, obj2) => {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (key of Object.keys(obj1)) {
if (typeof obj1[key] === "object") {
if (!areObjectKeysValuesEqual(obj1[key], obj2[key])) {
return false;
}
}
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true;
};
const result = areObjectKeysValuesEqual(window.foo2, window.bar2);
_.isEqual(window.foo3, window.bar3)
const areObjectKeysValuesEqual = (obj1, obj2) => {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (key of Object.keys(obj1)) {
if (typeof obj1[key] === "object") {
if (!areObjectKeysValuesEqual(obj1[key], obj2[key])) {
return false;
}
}
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true;
};
const result = areObjectKeysValuesEqual(window.foo3, window.bar3);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEqual 1 | |
Custom Recursive Function 1 | |
_.isEqual 2 | |
Custom Recursive Function 2 | |
_.isEqual 3 | |
Custom Recursive Function 3 |
Test name | Executions per second |
---|---|
_.isEqual 1 | 596294.6 Ops/sec |
Custom Recursive Function 1 | 324546.4 Ops/sec |
_.isEqual 2 | 390322.2 Ops/sec |
Custom Recursive Function 2 | 158911.4 Ops/sec |
_.isEqual 3 | 244106.0 Ops/sec |
Custom Recursive Function 3 | 62041.2 Ops/sec |
Let's break down the benchmark and its results.
Benchmark Description
The benchmark compares the performance of two approaches to check if two objects are equal:
isEqual
: A widely-used utility function from the Lodash library that checks for deep equality between two objects.Options Compared
The benchmark compares the performance of each approach on three different test cases:
.isEqual
1: Tests Lodash isEqual
on two objects with one level of nested structure (foo1
and bar1
)..isEqual
2: Tests Lodash isEqual
on two objects with two levels of nested structure (foo2
and bar2
), which is not present in the provided benchmark data.foo3
and bar3
).foo4
and bar4
), which is not present in the provided benchmark data.foo5
and bar5
).Latest Benchmark Results
The latest benchmark results show that:
.isEqual
1 outperforms Custom Recursive Function 1, indicating that Lodash's implementation is faster..isEqual
2 does not have a corresponding test case in the provided data.Key Takeaways
The benchmark highlights the performance differences between Lodash's isEqual
and a custom recursive implementation of object equality checks. The results suggest that:
These findings can be useful for developers who need to optimize performance-critical code involving object equality checks.