<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script type="text/javascript">
window.lodash = _;
_ = null;
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js'></script>
<script type="text/javascript">
window.underscore = _;
_ = null;
</script>
<script src="https://cdn.jsdelivr.net/npm/react-fast-compare@latest/index.js"></script>
// 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 } } };
// 4 levels deep
window.foo4 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } } };
window.bar4 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 4 } } } };
// 5 levels deep
window.foo5 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } } } };
window.bar5 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 4 } } } } };
lodash.isEqual(window.foo5, window.bar5)
underscore.isEqual(window.foo5, window.foo5)
JSON.stringify(window.foo5) === JSON.stringify(window.bar5);
equal(window.foo5, window.foo5)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash Level 5 | |
underscore Level 5 | |
JSON.stringify Level 5 | |
react-fast-compare Level 5 |
Test name | Executions per second |
---|---|
lodash Level 5 | 460204.7 Ops/sec |
underscore Level 5 | 78929840.0 Ops/sec |
JSON.stringify Level 5 | 1742588.5 Ops/sec |
react-fast-compare Level 5 | 92382880.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Description
The benchmark is designed to compare the performance of different libraries and approaches for equality checks in JavaScript. The tests are based on a specific use case where two objects (foo5
and bar5
) are compared using various methods:
isEqual
isEqual
equal
===
The benchmark also includes a control test that converts the objects to JSON strings for comparison.
Library and Approaches Comparison
Here's a brief overview of each library and approach:
isEqual
function checks if two objects are equal by recursively comparing their properties.isEqual
function works similarly to Lodash's, but with some differences in implementation.equal
function uses a combination of techniques, including hashing and caching, to quickly determine if two objects are equal.===
operator.Pros and Cons
Here's a brief summary of each approach:
Additional Considerations
When choosing an equality check approach, consider the following factors:
===
might suffice. More complex objects may require Lodash or Underscore.isEqual
function provides a clear and concise API.Other Alternatives
If you're looking for alternative libraries or approaches, consider:
These alternatives might not offer the same level of performance as React Fast Compare, but they can provide a more straightforward and maintainable solution for many use cases.