Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs double spread
(version: 0)
compare object clone methods where single level of recursion is needed
Comparing performance of:
Lodash cloneDeep vs Double spread operation
Created:
3 years ago
by:
Guest
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 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; };
Tests:
Lodash cloneDeep
testCopy = _.cloneDeep(testArray);
Double spread operation
testCopy = {...testArray, testObject: {...testArray.testObject}}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Double spread operation
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):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark test case, specifically comparing the performance of two object cloning methods: Lodash's `cloneDeep` function and double spread operation. **Test Case 1: Lodash cloneDeep** In this test case, the script prepares an array of objects (`testArray`) with nested properties, including arrays. The `cloneDeep` function from Lodash is then used to create a deep copy of the entire `testArray`. This method recursively clones all nested objects and arrays. **Test Case 2: Double Spread Operation** In this test case, the script uses the double spread operation (also known as rest parameter spreading) to create a new object that contains all properties from the original `testArray`, including nested objects and arrays. The resulting object is then assigned to the `testCopy` variable. **Comparison of Options** 1. **Lodash cloneDeep**: This method provides a robust and efficient way to clone complex objects with nested properties. It handles various types of data, such as dates, numbers, and objects, correctly. * Pros: + Robustness in handling complex data structures + Easy to use and maintain * Cons: + Requires including the Lodash library in the project (depending on the version) 2. **Double Spread Operation**: This method uses JavaScript's rest parameter spreading feature to create a new object with all properties from the original array. * Pros: + Lightweight and does not require additional libraries + Easy to implement * Cons: + May not work correctly for deeply nested objects or arrays + Can be slower than Lodash's `cloneDeep` due to its simplicity **Library: Lodash** Lodash is a popular JavaScript library that provides various utility functions, including `cloneDeep`. The `cloneDeep` function recursively clones all properties of an object, ensuring that nested objects and arrays are properly copied. **Special JS Feature or Syntax: Double Spread Operation** The double spread operation (rest parameter spreading) was introduced in ECMAScript 2015 (ES6). It allows you to use the syntax `{...obj}` to create a new object with all properties from an existing object, `obj`. **Alternative Methods** Other alternatives for cloning objects or arrays include: * `Array.prototype.slice()`: Creates a shallow copy of an array. * `Object.assign()`: Creates a shallow copy of an object. * `JSON.parse(JSON.stringify(obj))`: Creates a deep copy of an object.
Related benchmarks:
Lodash cloneDeep vs native cloneDeep vs dom cloneDeep
Lodash clone VS Lodash cloneDeep VS Spread operator with array of objects
array shallow clone comparison narrowed
array of objects shallow clone comparison narrowed
Lodash clone deep object array vs string array
Comments
Confirm delete:
Do you really want to delete benchmark?