Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Deep Copy
(version: 1)
Comparing performance of:
Lodash copydeep vs Spread operator
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here--> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
Script Preparation code:
var arr = new Array(1000).fill(null).map(() => ({ id: Math.random(), event_id: `evt_${Math.random()}`, calendar_id: `cal_${Math.random()}`, event_json: { title: "Test Event", description: "Test Description", startDate: "2024-01-01", endDate: "2024-01-02", location: { address: "123 Test St", lat: 44.4582983, lng: -93.1616132 }, attendees: [ { email: "test1@test.com", response: "accepted" }, { email: "test2@test.com", response: "pending" } ] }, is_latest: 1, start_date: "2024-01-01", end_date: "2024-01-02" }));
Tests:
Lodash copydeep
myCopy = _.cloneDeep(arr);
Spread operator
myCopy = {...arr};
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash copydeep
Spread operator
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:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash copydeep
475.4 Ops/sec
Spread operator
45423.1 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares two different methods for creating a deep copy of an array of objects in JavaScript. The focus is on performance and efficiency when copying complex data structures. ### Benchmark Overview 1. **Data Preparation**: - An array (`arr`) of 1000 objects is created, where each object contains various properties like `id`, `event_id`, and a nested object `event_json`. This structure includes several layers of depth, making it suitable for testing deep copying methods. 2. **Testing Methods**: - **Lodash Deep Copy**: ```javascript myCopy = _.cloneDeep(arr); ``` This uses the `_.cloneDeep` function from the Lodash library, which is a utility library that provides various functions for manipulating arrays, numbers, objects, strings, etc. The `cloneDeep` method creates a deep copy of the input value, ensuring that nested objects are also duplicated. - **Spread Operator**: ```javascript myCopy = {...arr}; ``` This uses the spread syntax, a feature in JavaScript that allows iterable objects (like arrays) to be expanded into individual elements. However, it's essential to note that the spread operator does not create a deep clone; it only creates a shallow copy of the top-level properties of the array. For nested objects, it copies the references, which can lead to unintended mutations if the nested objects are altered after the spread operation. ### Direct Comparisons - **Performance**: - The **Spread Operator** achieved significantly higher performance with **45423 Executions per Second**. - The **Lodash Deep Copy** performed substantially slower, at **475 Executions per Second**. ### Pros and Cons - **Lodash Deep Copy (Pros)**: - Creates a true deep copy of nested objects, making it safer for modifying deep structures without affecting the original data. - **Lodash Deep Copy (Cons)**: - Slower performance, especially with large or deeply nested data structures. - Additional dependency on the Lodash library. - **Spread Operator (Pros)**: - High performance and speed for shallow copies, making it efficient for one-level deep structures. - No external library dependency; part of standard JavaScript. - **Spread Operator (Cons)**: - Does not handle deep structures correctly; nested objects retain references to the originals, which can lead to unintentional side effects if the nested objects are modified. ### Other Considerations - **Use Case Suitability**: - If you need to create deep copies frequently, especially with complex structures, using Lodash or a similar library is advisable despite its lower speed. - For shallow copies or simpler structures where performance is critical, the spread operator can be preferable. ### Alternatives - **JSON Methods**: Using `JSON.parse(JSON.stringify(arr))` can be another method to perform a deep copy. However, this has limitations such as not handling functions, undefined values, and circular references. - **Manual Implementation**: Custom deep clone functions can be written to address specific use cases, although this requires careful handling of various data types and may introduce complexity. - **Other Libraries**: Aside from Lodash, libraries like `Rambda` or `immer` provide similar functionalities and could be considered based on project requirements. Overall, the choice between these methods depends on the specific needs for deep cloning versus performance in your application context.
Related benchmarks:
Nested vs Cascade
Find events real
Find events real end
json and
array iteration vs _.each vs map vs for..of vs for loop AP
array iteration vs _.each vs map vs for..of vs for loop AP1
Shiogui - Push x Reduce Logged
Lodash Deep Copy2
Lodash Deep Copy3
Comments
Confirm delete:
Do you really want to delete benchmark?