<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)
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);
--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 | 2654560.2 Ops/sec |
JSON.stringify Level 1 | 2738258.0 Ops/sec |
_.isEqual Level 2 | 1324693.9 Ops/sec |
JSON.stringify Level 2 | 2757419.5 Ops/sec |
_.isEqual Level 3 | 889800.6 Ops/sec |
JSON.stringify Level 3 | 2725385.5 Ops/sec |
Measuring the performance of JavaScript microbenchmarks is crucial for developers to optimize their code and ensure it runs efficiently.
Benchmark Definition:
The benchmark is designed to test the performance of Lodash's isEqual
function on objects with nested structures.
Options Compared:
Pros and Cons of Each Approach:
isEqual
:Library:
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object transformation, and more. In this benchmark, Lodash's isEqual
function is being tested for its performance on objects with complex nested structures.
Special JS Features/Syntax: None mentioned in the provided code.
Benchmark Preparation Code:
The script preparation code creates three objects (foo1
, bar1
, foo2
, bar2
, and foo3
, bar3
) with increasingly nested structures. These objects are then used to test the performance of Lodash's isEqual
function using both the direct comparison method and JSON.stringify.
Other Alternatives: