<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
window.foo = { a: { b: ['cat', 'dog', 'bird']}, c: { d: [42, '42', { val: true}]}};
window.bar = { a: { b: ['cat', 'dog', 'bird']}, c: { d: [42, '42', { val: 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 | 305065.0 Ops/sec |
JSON.stringify | 338868.9 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Benchmark Purpose: The benchmark is designed to compare the performance of two approaches for equality comparison on deep objects:
_.isEqual
function.JSON.stringify
and comparing the resulting strings.Options Compared:
JSON.stringify
method to convert both objects into strings and then compares these strings for equality.Pros and Cons:
Library:
The JSON.stringify
method is a built-in JavaScript function that converts an object into a JSON string. It's used to compare the string representations of two objects.
Special JS Feature/Syntax: None mentioned explicitly, but note that Lodash's _.isEqual uses internal optimizations and custom comparisons for specific data types (e.g., dates, regular expressions).
Other Alternatives:
JSON.stringify + Comparison
, but with more overhead due to the repeated calls.Benchmark Preparation:
The benchmark preparation code sets up two test objects, window.foo
and window.bar
, which are used as inputs for the test cases. The Lodash library is included in the HTML document using a CDN link.
Overall, this benchmark provides a fair comparison between two approaches to equality checking on deep objects, allowing users to evaluate performance trade-offs and choose the best approach for their specific use case.