<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dequal@2.0.2/dist/index.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, 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)
dequal.dequal(window.foo1, window.bar1)
JSON.stringify(window.foo1) === JSON.stringify(window.bar1);
_.isEqual(window.foo2, window.bar2)
dequal.dequal(window.foo2, window.bar2)
JSON.stringify(window.foo2) === JSON.stringify(window.bar2);
_.isEqual(window.foo3, window.bar3)
dequal.dequal(window.foo3, window.bar3)
JSON.stringify(window.foo3) === JSON.stringify(window.bar3);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Level 1 isEqual | |
Level 1 dequal | |
Level 1 stringify | |
Level 2 isEqual | |
Level 2 dequal | |
Level 2 stringify | |
Level 3 isEqual | |
Level 3 dequal | |
Level 3 stringify |
Test name | Executions per second |
---|---|
Level 1 isEqual | 3755401.0 Ops/sec |
Level 1 dequal | 21424006.0 Ops/sec |
Level 1 stringify | 3057170.2 Ops/sec |
Level 2 isEqual | 1787579.5 Ops/sec |
Level 2 dequal | 9994106.0 Ops/sec |
Level 2 stringify | 3518258.5 Ops/sec |
Level 3 isEqual | 1190490.8 Ops/sec |
Level 3 dequal | 5763307.5 Ops/sec |
Level 3 stringify | 3511011.2 Ops/sec |
Benchmark Overview
The provided JSON represents a benchmark test for comparing the performance of three JavaScript functions: _.isEqual, dequal, and JSON.stringify. The tests are designed to measure the execution time of these functions on two different objects with varying levels of nesting.
Options Compared
The three options being compared are:
Pros and Cons of Each Approach
Performance Implications
The performance results show that _.isEqual is generally the fastest option across all test cases. This is likely due to its optimized implementation and extensive testing history. However, dequal might provide better performance in specific scenarios where the test author has implemented an efficient algorithm.
JSON.stringify is significantly slower than both _.isEqual and dequal due to its overhead of converting objects to JSON strings and comparing them.
Conclusion
The choice of which option to use depends on the specific requirements of your project. If you need a robust, widely tested solution with good performance, _.isEqual might be the best choice. However, if you have control over the implementation and can optimize it for better performance, dequal could be a viable alternative. JSON.stringify is generally only suitable for very simple equality checks on JSON strings.