<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
window.foo = ['cat', 'dog', 'bird'];
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 | 7981812.0 Ops/sec |
JSON.stringify | 5315959.0 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Purpose: The benchmark compares the performance of two approaches to check for equality between two arrays of strings:
_.isEqual
from Lodash libraryJSON.stringify()
with shallow equality comparison (===
)Lodash _.isEqual:
_._isEqual
function is a utility function that checks if two values are equal.Pros:
Cons:
JSON.stringify() with shallow equality comparison:
JSON.stringify()
to convert both arrays into a string representation and then compares these strings using the ===
operator.JSON.stringify()
only considers primitive values, it will not recursively traverse objects or arrays.Pros:
Cons:
Other considerations:
['cat', 'dog', 'bird']
) for both arrays. This is a simple and common use case, but it might not be representative of more complex scenarios._._isEqual
function has a built-in tolerance mechanism to account for minor differences in values due to floating-point arithmetic or string formatting issues.Browser-specific details:
In this benchmark, the _.isEqual
test uses Chrome 83 on Windows, which might have some optimizations or bugs that affect performance. The JSON.stringify
test also runs on the same browser and device platform, but without any additional library or configuration.
Alternatives and further options:
_._isEqual
, you could use built-in JavaScript functions like ===
(shallow equality) or more advanced checks like Object.is()
or Array.prototype.every()
.JSON.stringify
approach, you might consider using more advanced string comparison methods, such as string comparison techniques or even regular expressions to handle edge cases.