Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash isEqual vs JSON stringify1
(version: 1)
Comparing performance of:
Lodash cloneDeep vs Json clone
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var MyObject = { "location": { "name": "Lobby", "id": "70ea2d94-2d21-4d9e-ae4d-bd3b5fa76562" }, "question": { "text": "Which setting worked best for you today?", "id": "0033a643-ae51-4115-b392-a3939a54a06f", "answerOrder": [ "b564a8fd-ec39-4fdd-9531-e7875d5e43e2", "cd8bb606-4b94-42c5-b76c-bd67df09caa6", "0ae345a5-eb4a-4415-aab1-de3789c6700e", "52a60c7f-f5bb-4be2-af56-59c2dd2e49d4" ] }, "answers": [ { "text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/cd8bb606-4b94-42c5-b76c-bd67df09caa6/31c403f4-07e7-4294-8409-3cbc5379beb3.jpg", "ansType": 1, "id": "cd8bb606-4b94-42c5-b76c-bd67df09caa6", "tags": "", "link": "", "linkDescription": "", "linkAction": "embed" }, { "text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/b564a8fd-ec39-4fdd-9531-e7875d5e43e2.png", "ansType": 1, "id": "b564a8fd-ec39-4fdd-9531-e7875d5e43e2", "tags": "" }, { "text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/0ae345a5-eb4a-4415-aab1-de3789c6700e.png", "ansType": 1, "id": "0ae345a5-eb4a-4415-aab1-de3789c6700e", "tags": "" }, { "text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/52a60c7f-f5bb-4be2-af56-59c2dd2e49d4/7737b24e-c45f-4517-a626-def4c2045ff9.jpg", "ansType": 1, "id": "52a60c7f-f5bb-4be2-af56-59c2dd2e49d4", "tags": "", "link": "", "linkDescription": "", "linkAction": "embed" } ] }; var MyObject2 = { "location": { "name": "Lobby", "id": "70ea2d94-2d21-4d9e-ae4d-bd3b5fa76562" }, "question": { "text": "Which setting worked best for you today?", "id": "0033a643-ae51-4115-b392-a3939a54a06f", "answerOrder": [ "b564a8fd-ec39-4fdd-9531-e7875d5e43e2", "cd8bb606-4b94-42c5-b76c-bd67df09caa6", "0ae345a5-eb4a-4415-aab1-de3789c6700e", "52a60c7f-f5bb-4be2-af56-59c2dd2e49d4" ] }, "answers": [ { "text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/cd8bb606-4b94-42c5-b76c-bd67df09caa6/31c403f4-07e7-4294-8409-3cbc5379beb3.jpg", "ansType": 1, "id": "cd8bb606-4b94-42c5-b76c-bd67df09caa6", "tags": "", "link": "", "linkDescription": "", "linkAction": "embed" }, { "text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/b564a8fd-ec39-4fdd-9531-e7875d5e43e2.png", "ansType": 1, "id": "b564a8fd-ec39-4fdd-9531-e7875d5e43e2", "tags": "" }, { "text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/0ae345a5-eb4a-4415-aab1-de3789c6700e.png", "ansType": 1, "id": "0ae345a5-eb4a-4415-aab1-de3789c6700e", "tags": "" }, { "text": "https://qranswerspdfs124808-dev.s3.amazonaws.com/public/answers/2583ae83-ec67-41c0-a837-f277e8440328/52a60c7f-f5bb-4be2-af56-59c2dd2e49d4/7737b24e-c45f-4517-a626-def4c2045ff9.jpg", "ansType": 1, "id": "52a60c7f-f5bb-4be2-af56-59c2dd2e49d4", "tags": "", "link": "", "linkDescription": "", "linkAction": "embed" } ] }; var myCopy = null;
Tests:
Lodash cloneDeep
_.isEqual(MyObject, MyObject2);
Json clone
JSON.stringify(MyObject) === JSON.stringify(MyObject2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
263366.9 Ops/sec
Json clone
301980.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the benchmark named **"Lodash isEqual vs JSON stringify1"**, two different approaches for comparing JavaScript objects are tested: `_.isEqual` from Lodash and the native `JSON.stringify` method. The aim is to evaluate the performance of these two methods in terms of execution speed. ### Test Cases 1. **Lodash `isEqual`** - **Benchmark Definition:** `_.isEqual(MyObject, MyObject2);` - **Library Used:** Lodash - **Purpose:** Lodash is a JavaScript library that provides utility functions for common programming tasks. The `_.isEqual` method deep compares two values to determine if they are equivalent. It will check not only the object references but also their properties and nested structures. - **Pros and Cons:** - **Pros:** - Accurately compares nested objects, arrays, and different data types. - Handles edge cases well, such as comparing types of elements and treating cyclical references appropriately. - **Cons:** - Can be slower due to its comprehensive checks, especially with large or deeply nested objects. - Introduces an additional dependency (Lodash) into the project. 2. **JSON `stringify`** - **Benchmark Definition:** `JSON.stringify(MyObject) === JSON.stringify(MyObject2);` - **Library Used:** None (native JavaScript method) - **Purpose:** The `JSON.stringify` method converts a JavaScript object into a JSON string. This string can then be compared using strict equality (===). - **Pros and Cons:** - **Pros:** - Simpler and faster for quick comparisons between objects, assuming that the order of properties is the same. - No additional libraries are needed, reducing overhead. - **Cons:** - Will fail to accurately compare objects if property order differs, as string representation can vary. - Does not handle complex data types like functions, dates, `undefined`, or circular references well; these will be ignored or throw errors. - Not capable of distinguishing between equivalent objects that contain the same data but in different structures. ### Performance Results Based on the benchmark results: - **JSON.stringify:** Executions per second: **301,980.19** - **Lodash `isEqual`:** Executions per second: **263,366.87** This indicates that the `JSON.stringify` method is significantly faster for the input objects being tested when compared to Lodash's `isEqual`. ### Other Considerations - The choice between these two methods depends on the specific requirements of the application. If accuracy in comparing object structures is crucial, the Lodash method should be used despite its slower performance. For lightweight checks where performance is a priority and property order is consistent, `JSON.stringify` suffices. - Alternatives exist beyond these options: - **Deep Comparison Libraries:** There are other libraries, such as `deep-equal` or `fast-deep-equal`, which may offer similar functionality with different performance characteristics. - **Manual Comparison Logic:** Implementing a custom deep comparison function tailored for specific use cases may also be an option if performance is critical and the structure is known. - **Using ES6 methods like `Map` or `Set` for specific cases** where unique groupings or mappings of objects are needed, especially when the data sets are of simpler structures. In conclusion, this benchmark provides valuable insights into the trade-offs between accuracy and performance when comparing JavaScript objects, guiding developers in choosing the right approach suited for their specific scenarios.
Related benchmarks:
Lodash cloneDeep vs JSON Clone 121212121
JSON.stringify vs Text (huge json dummy)
lodash clonedeep vs json.parse(stringify())
Test Lodash by Huy le
Test Lodash by Huy le 1
MD5 vs JSON.stringify
object-hash vs JSON.stringify_harsh_gangar
JSON.parse vs StructuredClone Huge Object
Lodash isEqual vs JSON stringify11
Comments
Confirm delete:
Do you really want to delete benchmark?