<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 } } }, { 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 } } }, { 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 } } }, { 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 } } }, { 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 } } }, { 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 } } }, { 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 | 3579050.0 Ops/sec |
JSON.stringify Level 1 | 2020414.1 Ops/sec |
_.isEqual Level 2 | 2068395.8 Ops/sec |
JSON.stringify Level 2 | 2013227.0 Ops/sec |
_.isEqual Level 3 | 1461128.1 Ops/sec |
JSON.stringify Level 3 | 1999769.6 Ops/sec |
Let's break down the provided JSON data and explain what is being tested.
Benchmark Definition:
The benchmark defines three levels of nested objects in JavaScript arrays, represented by foo1
, bar1
, foo2
, bar2
, foo3
, and bar3
. The goal is to compare the performance of different approaches to test equality between these arrays.
Test Cases:
There are six test cases, each with a unique approach to test equality:
isEqual
function to compare the two arrays.JSON.stringify
method to convert both arrays to strings and then compares them for equality.Options being compared:
Pros and Cons of each approach:
Libraries used:
.isEqual
function)Special JavaScript feature/syntax:
None mentioned in the provided data.
Other considerations:
In summary, this benchmark compares the performance of two approaches to test equality between JavaScript arrays: Lodash's isEqual
function and explicit string conversion using JSON.stringify
. The results will help identify the fastest approach for testing array equality in different scenarios.