<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/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 | 835661.1 Ops/sec |
JSON.stringify Level 1 | 395615.1 Ops/sec |
_.isEqual Level 2 | 479152.8 Ops/sec |
JSON.stringify Level 2 | 378538.2 Ops/sec |
_.isEqual Level 3 | 350720.5 Ops/sec |
JSON.stringify Level 3 | 385043.2 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark measures the performance of Lodash's isEqual
function in comparing two objects with different structures (1 level deep, 2 levels deep, and 3 levels deep). The test also includes comparisons using JSON stringification (JSON.stringify
) to check if the same object is returned.
Test Cases
There are six test cases:
_.isEqual(window.foo1, window.bar1)
: Compares two objects with a single level of nested structure.JSON.stringify(window.foo1) === JSON.stringify(window.bar1)
: Uses JSON stringification to compare the two objects._.isEqual(window.foo2, window.bar2)
: Compares two objects with two levels of nested structure.JSON.stringify(window.foo2) === JSON.stringify(window.bar2)
: Uses JSON stringification to compare the two objects._.isEqual(window.foo3, window.bar3)
: Compares two objects with three levels of nested structure.JSON.stringify(window.foo3) === JSON.stringify(window.bar3)
: Uses JSON stringification to compare the two objects.Comparison Methods
The comparison methods used in the test cases are:
isEqual
function: This function recursively compares two objects and returns a boolean indicating whether they are equal.===
operator.Pros and Cons of Comparison Methods
isEqual
functionLibrary: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions, including isEqual
. The isEqual
function is used to compare two values and returns a boolean indicating whether they are equal.
Special JS Feature/Syntax: None
There are no special JS features or syntax used in this benchmark that would require additional explanation.