Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash _.cloneDeep() vs deppClone()
(version: 0)
Comparing performance of:
lodash vs own
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
class Foo { } function objectCopy(object) { if (!object) { return {}; } const copyObject = {}; for (const attribute in object) { if (Array.isArray(object[attribute])) { copyObject[attribute] = arrayCopy(object[attribute]); continue; } if (object[attribute] instanceof Object) { copyObject[attribute] = objectCopy(object[attribute]); continue; } copyObject[attribute] = object[attribute]; } return copyObject; } function arrayCopy(array) { if (!array) { return []; } const copyArray = []; for (let i = 0; i < array.length; i++) { if (Array.isArray(array[i])) { copyArray[i] = arrayCopy(array[i]); continue; } if (array[i] instanceof Object) { copyArray[i] = objectCopy(array[i]); continue; } copyArray[i] = array[i]; } return copyArray; } var user = { name: "Teszt Elek", address: { country: "Magyarország", county: "Borsod-Abaúj-Zemplém megye", city: "Miskolc", street: "Felsőruzsin", streetType: "körút", houseNumber: "21" }, job: { professional: "Szoftverfejleszto", company: { name: "Evosoft Hungary Kft.", group: { department: { name: "O5", code: "O5", teams: [ "1", "2", "3", "4" ], }, team: { name: "4", members: [ "Gipsz Jakab", "Teszt Elek", "John Doe", "Jane Doe", "Gipsz Jakab", "Teszt Elek", "John Doe", "Jane Doe", "Gipsz Jakab", "Teszt Elek", "John Doe", "Jane Doe", "Gipsz Jakab", "Teszt Elek", "John Doe", "Jane Doe", "Gipsz Jakab", "Teszt Elek", "John Doe", "Jane Doe" ], responibilityFor: { share: { file: "//Something.dat", binary: [ [2], [3], [5], [55], [1623], [1], [0], [1024], [2], [3], [5], [55], [1623], [1], [0], [1024], [2], [3], [5], [55], [1623], [1], [0], [1024] ], data: { binary: [ [2], [3], [5], [55], [1623], [1], [0], [1024], [2], [3], [5], [55], [1623], [1], [0], [1024], [2], [3], [5], [55], [1623], [1], [0], [1024] ], obj: { vami: "vami", hami: "hami", dami: { ini: "ini", ani: { ani: "ani" } } } } } } } } } } }
Tests:
lodash
_.cloneDeep(user)
own
objectCopy(user)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
own
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
5874.3 Ops/sec
own
85838.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is comparing two functions: `_.cloneDeep()` from the Lodash library and a custom function called `objectCopy()`. The test case uses a complex object with nested arrays and objects to create a realistic scenario for deep cloning. **Comparison of Options** There are two options being compared: 1. **Lodash's _.cloneDeep()**: This function is part of the Lodash library, which provides utility functions for functional programming. It's designed to recursively clone an object, creating new copies of all nested objects and arrays. 2. **Custom implementation: objectCopy()**: The custom implementation uses a recursive approach to create a deep copy of the object. It checks each property of the object to determine if it's an array or an object, and then recursively clones it. **Pros and Cons of Each Approach** 1. **Lodash's _.cloneDeep()**: * Pros: + Fast and efficient + Handles complex objects with nested arrays and objects + Reduces boilerplate code * Cons: + Adds external dependency (the Lodash library) + May have performance overhead due to the need for a separate library 2. **Custom implementation: objectCopy()**: * Pros: + No external dependencies + Can be optimized for specific use cases + Allows for custom logic and behavior * Cons: + Requires more boilerplate code + May have performance overhead due to the need for recursive cloning **Performance Comparison** The benchmark results show that the custom implementation (`objectCopy()`) has slightly better performance than Lodash's `_.cloneDeep()` on this specific test case, with an execution rate of 21574.38671875 compared to 21211.576171875. However, it's essential to note that the performance difference might not be significant in all cases, and other factors like memory usage, cache hits, and overall system load should also be considered when choosing between these two approaches. **Conclusion** In summary, Lodash's `_.cloneDeep()` is a convenient and efficient solution for deep cloning objects, but it comes with an external dependency. The custom implementation (`objectCopy()`) offers more control and flexibility but requires more boilerplate code and potential performance overhead. The choice ultimately depends on the specific requirements of your project and your personal preferences as a developer.
Related benchmarks:
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Lodash cloneDeep vs. Lodash clone vs. Array.slice() vs. Array.slice(0) vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign() vs Array.slice() vs Array.slice(0)
Comments
Confirm delete:
Do you really want to delete benchmark?