<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
window.foo = ['cat', 'dog', 'bird', {}, {a: 4, b: false, c: 'string', d: null}];
window.bar = ['cat', 'dog', 'bird', {}, {a: 4, b: false, c: 'string', d: null}];
_.isEqual(window.foo, window.bar)
JSON.stringify(window.foo) === JSON.stringify(window.bar);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEqual | |
JSON.stringify |
Test name | Executions per second |
---|---|
_.isEqual | 755780.1 Ops/sec |
JSON.stringify | 1208385.0 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition:
The benchmark tests two approaches for comparing equality between two arrays, window.foo
and window.bar
, which contain both primitive values (strings) and objects.
Options being compared:
===
operator to compare the stringified representations of the two arrays, which is equivalent to performing a deep equality check.Pros and Cons:
Library:
In the provided benchmark definition, Lodash is used as a library to provide the isEqual
function. The library's purpose is to simplify common programming tasks by providing a collection of reusable functions.
Special JS feature/syntax:
None mentioned in this specific benchmark.
Other alternatives:
For this particular use case, you could also consider using other libraries or custom implementations that offer deep equality checking, such as:
However, these alternatives might not be suitable for all use cases, and the choice ultimately depends on the specific requirements of your project.