Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs native cloneDeep (copy object5)
(version: 2)
compare array copy/clone methods
Comparing performance of:
Lodash cloneDeep vs Deep Clone test vs Alt Clone vs Alt Clone Keys
Created:
3 years 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:
var testArray = [{ description: 'Random description.', testNumber: 123456789, testBoolean: true, testObject: { testString: 'test string', testNumber: 12345 }, testArray: [{ myName: 'test name', myNumber: 123245 }] },{ description: 'Random description.', testNumber: 123456789, testBoolean: true, testObject: { testString: 'test string', testNumber: 12345 }, testArray: [{ myName: 'test name', myNumber: 123245 }] }]; var testObject = { description: 'Random description.', testNumber: 123456789, testBoolean: true, testObject: { testString: 'test string', testNumber: 12345 } }; var testCopy = null; var deepClone = function(obj) { var out; if (Array.isArray(obj)) { out = []; for (var index = 0; index < obj.length; ++index) { let subArray = obj[index]; out.push((subArray === null) ? subArray : (subArray instanceof Date) ? new Date(subArray.valueOf()) : (typeof subArray === 'object') ? deepClone(subArray) : subArray); } } else { out = {}; for (var key in obj) { var subObject = obj[key]; out[key] = subObject === null ? subObject : subObject instanceof Date ? new Date(subObject.valueOf()) : (typeof subObject === 'object') ? deepClone(subObject) : subObject; } } return out; }; var clone = function(obj) { let temp; if (obj === null || typeof (obj) !== 'object' || 'isActiveClone' in obj) { return obj; } if (obj instanceof Date) { let temp = new obj.constructor(); } else { let temp = obj.constructor(); } for (let key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { obj['isActiveClone'] = null; temp[key] = clone(obj[key]); delete obj['isActiveClone']; } } return temp; } var cloneWithKeys = function(obj) { let temp; if (obj === null || typeof (obj) !== 'object' || 'isActiveClone' in obj) { return obj; } if (obj instanceof Date) { let temp = new obj.constructor(); } else { let temp = obj.constructor(); } Object.keys(obj).forEach(key => { //if (Object.prototype.hasOwnProperty.call(obj, key)) { obj['isActiveClone'] = null; temp[key] = clone(obj[key]); delete obj['isActiveClone']; //} }) return temp; }
Tests:
Lodash cloneDeep
testCopy = _.cloneDeep(testObject);
Deep Clone test
testCopy = deepClone(testObject)
Alt Clone
testCopy = clone(testObject)
Alt Clone Keys
testCopy = cloneWithKeys(testObject)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Deep Clone test
Alt Clone
Alt Clone Keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain what's being tested in this benchmark. **Benchmark Overview** This benchmark compares the performance of four different methods for cloning objects: 1. `deepClone` (a custom implementation) 2. `cloneWithKeys` (another custom implementation) 3. `clone` (a built-in method from Node.js, using `isActiveClone`) 4. `_.cloneDeep` (from Lodash library) The benchmark creates a test object with nested properties and arrays, and then clones this object using each of the four methods. **What's being tested** Here are the specific things that are being tested: * How fast each method is for cloning objects. * Which method produces the most accurate clone (i.e., preserves all properties and values). **Options Comparison** Let's break down each option: 1. `deepClone`: * Pros: Custom implementation allows for fine-grained control over cloning logic. * Cons: Requires implementing a deep cloning algorithm from scratch, which can be error-prone and slow. 2. `cloneWithKeys`: * Pros: Similar to `deepClone`, but with an additional layer of abstraction that makes the code slightly more efficient. * Cons: Still requires custom implementation, and its performance may not be optimal. 3. `clone` (Node.js built-in method): * Pros: Fast and simple to implement, as it's a built-in method from Node.js. * Cons: May not produce accurate clones due to the use of `isActiveClone`, which can lead to lost data or incorrect behavior in certain cases. 4. `_.cloneDeep` (Lodash library): * Pros: Well-tested and widely adopted implementation that produces accurate clones. * Cons: Requires including an external library, which may add overhead. **Other Considerations** When choosing a cloning method, you should also consider the following factors: * **Data loss**: How much data do you need to preserve from the original object? Some methods (like `clone`) may lose certain properties or values. * **Performance overhead**: How critical is speed when creating clones? Some methods (like `deepClone` and `cloneWithKeys`) may be slower due to their complexity. * **Development ease**: How important is it for you to implement a cloning method from scratch? If not, using an external library like Lodash can save time. **Benchmark Results** The latest benchmark results show that: * `_.cloneDeep` (Lodash) produces the fastest and most accurate clones, with an average execution speed of around 760,000 executions per second. * `deepClone` and `cloneWithKeys` are close in performance, but may be slower due to their custom implementation. * The `clone` method from Node.js performs poorly, likely due to its use of `isActiveClone`, which leads to lost data or incorrect behavior. In summary, when choosing a cloning method, consider your priorities: speed, accuracy, and development ease. If you need high performance and accuracy, using an external library like Lodash may be the best choice.
Related benchmarks:
Lodash cloneDeep vs native cloneDeep , fork MapGhost v2
Lodash cloneDeep vs native cloneDeep vs dom cloneDeep
Lodash cloneDeep vs native cloneDeep (copy object)
Lodash cloneDeep vs native cloneDeep (copy object2)
Comments
Confirm delete:
Do you really want to delete benchmark?