Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.isEqual vs JSON.stringify Equality Comparison for json objects
(version: 1)
Test on isEqual performance
Comparing performance of:
_.isEqual vs JSON.stringify
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
window.foo = { "items": { "item": [ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, ... ] } }; window.bar = { "items": { "item": [ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, ... ] } };
Tests:
_.isEqual
_.isEqual(window.foo, window.bar)
JSON.stringify
JSON.stringify(window.foo) === JSON.stringify(window.bar);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.isEqual
JSON.stringify
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.isEqual
5330355.0 Ops/sec
JSON.stringify
4622996.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided compares two different methods for checking equality of two JSON-like objects in JavaScript. Specifically, it tests the performance of: 1. **Lodash's `isEqual` method**: `_.isEqual(window.foo, window.bar)` 2. **JSON.stringify method**: `JSON.stringify(window.foo) === JSON.stringify(window.bar)` ### Options Compared 1. **`_.isEqual`**: - **Library**: Lodash is a popular utility library in JavaScript that provides functions for common programming tasks, including deep comparison of objects. The `isEqual` method offers a thorough comparison that checks if the two objects contain equivalent values, even if the objects are not the same reference. - **Pros**: - Accurate deep comparison for complex objects. - Handles various types of values (arrays, objects, dates, etc.) and takes care of edge cases. - **Cons**: - Performance may degrade in case of very large objects, as it deeply traverses the entirety of object structures. - Requires the Lodash library, which adds some weight to projects if not already being used. 2. **`JSON.stringify` Comparison**: - **Method**: This approach converts both objects to JSON strings and compares the resulting strings for equality. - **Pros**: - Simplicity of implementation; no additional libraries are required. - Straightforward for objects that are serialized without circular references. - **Cons**: - Performance can suffer for large or nested objects due to string manipulation. - It may not accurately reflect object equality if the object properties are not in the same order, as stringified output can differ depending on property order (even if the objects are logically equivalent). - Fails for objects with non-serializable properties (like functions, `undefined`, and symbols), which may lead to misleading results. ### Alternative Methods Several other alternatives exist for comparing objects in JavaScript: 1. **`Object.is`**: This method is primarily used for checking primitive values and distinguishing between `-0` and `+0`, but not for object comparison. 2. **Shallow Comparison**: A quick check that verifies whether objects have identical property values at a single level, often implemented using simple loops or `Object.keys`. 3. **Custom Comparison Function**: For tailored needs, developers can write their own comparison logic to handle specific cases or types that matter for their application context. 4. **Other Libraries**: In addition to Lodash, libraries such as `deep-equal`, `deep-eql`, or `fast-equals` provide alternative implementations for deep comparison suitable for specific use cases. ### Conclusion This benchmark aims to assess which of these methods performs better when comparing complex JSON-like structures. The results indicate that Lodash's `isEqual` method has a better performance (3,041,769 executions per second) compared to the JSON.stringify method (2,621,830 executions per second) in this particular test case, likely due to its efficient handling of complex types and structures inherent in JavaScript objects. Software engineers should weigh the pros and cons of each method based on the specific requirements of their application, such as performance needs, object depth, and whether they prefer minimal dependencies.
Related benchmarks:
fork Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings.
json performance
Deep merge lodash vs ramda vs deepmerge vs json clone
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings. 123123123
cloneDeep vs JSON stringify + parse (structured arr)
Deep merge lodash vs ramda vs deepmerge vs json clone 3
Lodash.isEqual vs JSON.stringify Equaliparison for large json
Lodash.isEqual vs JSON.stringify Equality Comparison for Deep Array of Objects
deep json spread vs flat
Comments
Confirm delete:
Do you really want to delete benchmark?