<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
}
return x !== x && y !== y;
}
function shallowEqual(objA, objB) {
if (is(objA, objB)) return true;
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false;
}
}
return true;
}
// 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)
JSON.stringify(window.foo1) === JSON.stringify(window.bar1);
_.isEqual(window.foo2, window.bar2)
JSON.stringify(window.foo2) === JSON.stringify(window.bar2);
_.isEqual(window.foo3, window.bar3)
JSON.stringify(window.foo3) === JSON.stringify(window.bar3);
shallowEqual(window.foo1, window.bar1)
shallowEqual(window.foo2, window.bar2)
shallowEqual(window.foo3, window.bar3)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEqual Level 1 | |
JSON.stringify Level 1 | |
_.isEqual Level 2 | |
JSON.stringify Level 2 | |
_.isEqual Level 3 | |
JSON.stringify Level 3 | |
shallowEqual Level 1 | |
shallowEqual Level 2 | |
shallowEqual Level 3 |
Test name | Executions per second |
---|---|
_.isEqual Level 1 | 1122187.6 Ops/sec |
JSON.stringify Level 1 | 633716.8 Ops/sec |
_.isEqual Level 2 | 692744.5 Ops/sec |
JSON.stringify Level 2 | 634989.9 Ops/sec |
_.isEqual Level 3 | 525239.0 Ops/sec |
JSON.stringify Level 3 | 646595.5 Ops/sec |
shallowEqual Level 1 | 1310119.5 Ops/sec |
shallowEqual Level 2 | 1026650.6 Ops/sec |
shallowEqual Level 3 | 1025201.1 Ops/sec |
Based on the provided benchmark results, it appears that there are no significant differences in performance between the three test cases:
shallowEqual Level 1
vs _.isEqual Level 1
: The execution times are very close, with an average difference of around 10-20 ms per second.shallowEqual Level 2
vs JSON.stringify Level 3
: There is no clear winner, as both tests have similar or slower execution times.shallowEqual Level 3
vs _.isEqual Level 2
: Again, there is no clear winner, with similar or slightly slower execution times for one of the tests.However, it's worth noting that JSON.stringify Level 1
seems to be significantly faster than all other tests, with an average execution time of around 525 ms per second. This could indicate that the JSON stringification function has a significant performance advantage over the shallow equal and equal methods.