Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs json parse v3
(version: 1)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs structured Clone
Created:
one year ago
by:
Registered User
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:
const myObject = { "event": "preferences_changed", "data": { "preferences": "[{\"user_id\":\"ыпукнунукр\",\"category\":\"direct_channel_show\",\"name\":\"фвыфпуцкнкнуцк\",\"value\":\"true\"},{\"user_id\":\"смчсмчсмсмчсмчм\",\"category\":\"channel_open_time\",\"name\":\"аываываываыва\",\"value\":\"1740485407949\"}]" }, "broadcast": { "omit_users": null, "user_id": "фывфыввфывфывфывфыв", "channel_id": "", "team_id": "", "connection_id": "", "omit_connection_id": "" }, "seq": 2 }
Tests:
Lodash CloneDeep
myCopy = _.cloneDeep(myObject);
Json Clone
myCopy = JSON.parse(JSON.stringify(myObject));
structured Clone
myCopy = structuredClone(myObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash CloneDeep
Json Clone
structured Clone
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; rv:136.0) Gecko/20100101 Firefox/136.0
Browser/OS:
Firefox 136 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
549017.4 Ops/sec
Json Clone
320514.2 Ops/sec
structured Clone
221240.1 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the JSON compares three different methods for deep cloning a JavaScript object. The three methods being evaluated are: 1. **Lodash `cloneDeep`**: A utility function provided by the Lodash library for creating a deep clone of an object, which means creating a new object that is a copy of the original object, including nested objects. 2. **JSON serialize/deserialize**: This method involves converting an object to a JSON string and then parsing it back into a new object. This is often used as a quick way to deep clone objects, although it has limitations. 3. **Structured Clone**: This is a built-in JavaScript feature (available in modern browsers) that provides a way to create a deep clone of an object that can handle more complex data types, such as dates, maps, sets, and more. ### Comparison of Approaches #### 1. **Lodash CloneDeep** - **Pros**: - Handles a wide variety of data types, including nested objects, arrays, and others. - Well-tested and widely adopted in the JavaScript community. - **Cons**: - Requires the inclusion of an external library, which adds to the bundle size in a web application. - May be slower than simpler methods for small objects. #### 2. **JSON Clone** - **Pros**: - Simple to use and does not require additional libraries. - Fast for shallow objects or objects composed only of JSON-compatible types (numbers, strings, arrays, and plain objects). - **Cons**: - Can only clone objects that are fully serializable to JSON. This means it cannot handle undefined properties, functions, or special objects (e.g., `Map`, `Set`, `Date`). - Risks losing data if the object being cloned contains non-serializable types. #### 3. **Structured Clone** - **Pros**: - Can handle a wider variety of data types compared to JSON cloning, including `Date`, `Blob`, `File`, `Map`, `Set`, and others. - No need for additional libraries since it is built into the browser's JavaScript runtime. - **Cons**: - Browser support may not be universal across all older versions, though it is widely supported in modern browsers. - It may be slightly less performant than Lodash for very specific use cases or for certain types of objects. ### Benchmark Results According to the provided benchmark results: - **Lodash CloneDeep** had the highest execution rate at approximately **414,184 executions per second**. - **JSON Clone** followed closely with **326,628 executions per second**. - **Structured Clone** had the lowest performance with **205,227 executions per second**. ### Other Considerations When choosing between these methods, developers should weigh their specific use case requirements such as: - The complexity of the data being cloned (if it contains special types). - The importance of library size in web applications (potential impact on performance). - The environments where the code will run (browser compatibility). ### Alternatives Other alternatives not mentioned in the benchmark could include: - Implementing a custom cloning function, tailored to the specific structure of the object being cloned. - Using other libraries (e.g., Rambda, or custom utility functions) depending on the specific needs of the project. ### Conclusion Ultimately, the best method for deep cloning objects in JavaScript depends on the specific scenarios and application requirements, such as performance, compatibility, and ease of use. Selecting the suitable method requires careful consideration of the trade-offs presented by each approach.
Related benchmarks:
Lodash cloneDeep vs JSON Clonefff
Lodash 2
Deep Clone Performance - JSON vs Lodash vs Ramda vs Native2
Deep Clone Performance - JSON vs Lodash vs Ramda vs Native3
structured clone vs JSON
Different ways of creating objects
Different ways of creating objects 2
object literal & JSON.parse
JSON.stringify vs structuredCloneasasa
Comments
Confirm delete:
Do you really want to delete benchmark?