<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 } } };
// same
window.foo4 = { 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 } } };
_.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);
_.isEqual(window.foo4, window.bar4);
JSON.stringify(window.foo4) === JSON.stringify(window.bar4);
--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 | |
_.isEqual Level 4 | |
JSON.stringify Level 4 |
Test name | Executions per second |
---|---|
_.isEqual Level 1 | 740492.0 Ops/sec |
JSON.stringify Level 1 | 386676.5 Ops/sec |
_.isEqual Level 2 | 465469.3 Ops/sec |
JSON.stringify Level 2 | 387518.0 Ops/sec |
_.isEqual Level 3 | 333986.2 Ops/sec |
JSON.stringify Level 3 | 395726.3 Ops/sec |
_.isEqual Level 4 | 322794.0 Ops/sec |
JSON.stringify Level 4 | 398255.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is being tested?
The provided benchmark tests the equality comparison between two JSON objects, foo
and bar
, with varying levels of nesting (1, 2, 3, and 4 levels deep). The tests compare the results using both Lodash's isEqual
function and the ===
operator with JSON.stringify
.
Options compared:
There are four options being compared:
isEqual
function: This is a JavaScript utility library that provides a flexible way to compare objects.===
operator with JSON.stringify
: This is a built-in JavaScript operator that compares the contents of two values using a deep equality check, which converts both values to JSON strings.Pros and Cons:
isEqual
function:===
operator with JSON.stringify
:Why the different results?
The different results between Lodash's isEqual
function and the ===
operator with JSON.stringify
are due to their implementation differences. Lodash's isEqual
function checks for equality based on a set of predefined rules, which may not cover all possible edge cases. In contrast, the ===
operator with JSON.stringify
uses a deep comparison algorithm that converts both values to JSON strings and compares them byte-for-byte.
In this specific benchmark, the results show that Lodash's isEqual
function is slower than the ===
operator with JSON.stringify
, but it also provides more accurate results for complex object structures.