<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, 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)
2 === 2
_.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);
--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 |
Test name | Executions per second |
---|---|
_.isEqual Level 1 | 422053.6 Ops/sec |
JSON.stringify Level 1 | 673261632.0 Ops/sec |
_.isEqual Level 2 | 267784.1 Ops/sec |
JSON.stringify Level 2 | 269458.9 Ops/sec |
_.isEqual Level 3 | 199711.3 Ops/sec |
JSON.stringify Level 3 | 235305.6 Ops/sec |
I'll break down what's being tested, compared, and the pros/cons of different approaches.
What is being tested?
MeasureThat.net is testing the performance of _.isEqual
from Lodash library. The test cases cover three levels of nested objects:
foo1
and bar1
)foo2
and bar2
)foo3
and bar3
)The test also includes a simple comparison using the ===
operator on stringified versions of the objects.
What options are compared?
In this benchmark, only two options are being compared:
_isEqual
function===
operator comparisonPros and Cons:
Lodash's _isEqual
function:
Pros:
Cons:
Simple ===
operator comparison:
Pros:
Cons:
Other considerations:
The benchmark also includes a comparison using JSON.stringify
, which can be useful in certain situations, such as when comparing JSON data. However, this approach may not work well for all types of data and may lead to performance issues due to the overhead of stringifying objects.
Lodash library and its purpose:
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object transformation, and functional programming. The _isEqual
function is specifically designed to compare two values for equality, handling nested objects and arrays recursively.
If you're not familiar with Lodash, it's worth exploring its documentation and examples to learn more about the library and its features.
Special JS feature or syntax:
There are no special JavaScript features or syntax used in this benchmark. The code is straightforward and uses only standard JavaScript constructs.
Other alternatives to MeasureThat.net include: