Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash vs json parse
(version: 2)
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 = { level1Key1: { level2Key1: { level3Key1: "value1", level3Key2: "value2", level3Key3: "value3", level3Key4: "value4", level3Key5: "value5" }, level2Key2: { level3Key6: "value6", level3Key7: "value7", level3Key8: "value8", level3Key9: "value9", level3Key10: "value10" }, level2Key3: { level3Key11: "value11", level3Key12: "value12", level3Key13: "value13", level3Key14: "value14", level3Key15: "value15" } }, level1Key2: { level2Key4: { level3Key1: "value1", level3Key2: "value2", level3Key3: "value3", level3Key4: "value4", level3Key5: "value5" }, level2Key5: { level3Key6: "value6", level3Key7: "value7", level3Key8: "value8", level3Key9: "value9", level3Key10: "value10" }, level2Key6: { level3Key11: "value11", level3Key12: "value12", level3Key13: "value13", level3Key14: "value14", level3Key15: "value15" } }, level1Key3: { level2Key7: { level3Key1: "value1", level3Key2: "value2", level3Key3: "value3", level3Key4: "value4", level3Key5: "value5" }, level2Key8: { level3Key6: "value6", level3Key7: "value7", level3Key8: "value8", level3Key9: "value9", level3Key10: "value10" }, level2Key9: { level3Key11: "value11", level3Key12: "value12", level3Key13: "value13", level3Key14: "value14", level3Key15: "value15" } } }; console.log(tripleNestedObject);
Tests:
Lodash CloneDeep
myCopy = JSON.parse(JSON.stringify(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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
112235.8 Ops/sec
Json Clone
122024.3 Ops/sec
structured Clone
64570.6 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares three different methods for deep cloning a JavaScript object. Each method has its own advantages and disadvantages, as well as varying levels of performance, which is the focus of this benchmark. ### Methods and Comparisons 1. **Lodash's `cloneDeep`**: - **Code**: `myCopy = JSON.parse(JSON.stringify(myObject));` - **Description**: Lodash is a utility library that provides various functions for common programming tasks, including deep cloning of objects. However, in this benchmark, it appears the intention was to compare JSON serialization as an alternative rather than executing a true Lodash function call. - **Pros**: Can handle most cases of deep cloning effectively, including nested structures. - **Cons**: It may not work as expected with non-serializable data types such as functions, undefined values, or special objects (like Date). Performance-wise, it may be slower than pure JSON methods for structures that it can handle. 2. **JSON Clone**: - **Code**: `myCopy = JSON.parse(JSON.stringify(myObject));` - **Description**: This method involves converting the object to a JSON string and then parsing it back into an object. It is a common technique for creating a deep copy of a simplestructured object. - **Pros**: Fast and efficient for standard JavaScript objects; handles various layers of nested objects seamlessly. - **Cons**: Does not handle functions, `undefined`, `Date` objects, or circular references, leading to potential data loss in more complex scenarios. 3. **Structured Clone**: - **Code**: `myCopy = structuredClone(myObject);` - **Description**: The `structuredClone()` function is a built-in JavaScript method for creating a deep copy of an object, capable of handling complex data types (including dates, typed arrays, and other non-plain objects). - **Pros**: Superior handling of a variety of data types and object structures, including functions, circular references, sets, and maps. - **Cons**: As of this writing, the support across different browsers is still maturing, making it less consistent for cross-browser applications. ### Performance Results Summary Based on the benchmark results: - **JSON Clone** performed the best with approximately **122,024 executions per second**. - **Lodash CloneDeep** was close behind with about **112,236 executions per second**. - **Structured Clone** was significantly slower, with around **64,570 executions per second**. ### Considerations - **Use Cases**: The method chosen for cloning should be dictated by the specific use case. For example, if one needs to clone objects containing Dates or special functions, `structuredClone` is a better choice. For typical use cases where you are handling basic objects, JSON cloning may be sufficient and is the most performant. - **Error Handling**: Developers should be cautious about potential pitfalls associated with each cloning method, especially when dealing with complex data structures or types. - **Alternatives**: - Manually implementing deep copy through recursion, though this can be error-prone and adds complexity. - Using other libraries like Rambda or Immer, which have deep-cloning utilities similar to Lodash but with different capabilities and performance profiles. This benchmark provides valuable insights into cloning techniques, guiding developers in selecting the right approach based on performance requirements and object complexity.
Related benchmarks:
Test Immutable ToJS vs get
Quotes vs no quotes
Direct Array Access vs. New Local Constant Variable in A Loop
Direct Array Access vs. New Local Constant Variable in A Loop v.2
for... in VS Object.keys()
for... in VS Object.keys() VS Object.entries()
destructuring vs creating
For vs Map test 27/01/25
lodash vs json parse v2
Comments
Confirm delete:
Do you really want to delete benchmark?