<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
window.foo = ['cat', 'dog', 'frog'];
window.bar = ['cat', 'dog', 'bird'];
_.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 | 6508255.0 Ops/sec |
JSON.stringify | 5355684.5 Ops/sec |
Let's break down what's being tested in the provided JSON.
Benchmark Purpose:
The benchmark compares the performance of two approaches for equality comparison: using Lodash's isEqual
function and using JSON.stringify
. The test focuses on shallow array comparisons, specifically when the comparison is not equal.
Options Compared:
JSON.stringify
method on both arrays, comparing the resulting strings for equality. The idea is to convert the arrays to strings and then compare them.Pros and Cons:
JSON
object, which is a built-in part of most JavaScript engines.Library:
The JSON.stringify
method converts its argument(s) into a JSON string, which can be compared for equality. In this benchmark, it's used to compare the string representations of two shallow arrays.
Special JS Feature/Syntax:
There is no special JavaScript feature or syntax mentioned in the benchmark. However, using JSON.stringify
for comparison relies on the browser implementing it correctly and efficiently.
Benchmark Results:
The latest benchmark results show that Lodash's isEqual
function outperforms the JSON.stringify
approach in terms of execution time (measured in executions per second). This suggests that the custom implementation is slower due to the overhead of converting the arrays to strings and comparing them.
Other Alternatives:
every()
with a callback function.every()
, but returns as soon as it finds at least one element that doesn't pass the test.isEqual
.