Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep VS spread operator w/ extend
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Spread operator
Created:
6 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 MyObject = { description: 'a', myNumber: 123456789, myBoolean: true, deeper: { we: "must", go: "deeper" } }; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.extend(_.cloneDeep(MyObject), {more: "props"});
Spread operator
myCopy = {...MyObject, more: "props"};
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Spread operator
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
gemma2:9b
, generated one year ago):
This benchmark tests two different ways to create a copy of a JavaScript object: using Lodash's `cloneDeep` function and the spread operator (`...`). **Here's a breakdown:** * **Lodash `cloneDeep`:** This approach utilizes the popular Lodash library, specifically its `cloneDeep` function. Lodash is known for providing utility functions that simplify common JavaScript operations, including object cloning. In this case, it creates a deep copy of the original object (`MyObject`), meaning all nested objects are also copied recursively. The result is then extended using `_.extend`, which merges additional properties from another object (in this case, an object with the property "more: \"props\""). * **Spread Operator:** This approach leverages the spread syntax (`...`) introduced in ES6. It unpacks all the properties of the original object (`MyObject`) into a new object literal. Then, it directly adds the new property ("more: \"props\"") to this new object. **Pros and Cons:** * **Lodash `cloneDeep`:** * **Pros:** Handles nested objects effectively (deep copy), well-tested and widely used library. * **Cons:** Adds external dependency on Lodash, potentially slower for simple cases compared to the spread operator. * **Spread Operator:** * **Pros:** Conciseness, native to JavaScript, generally faster for simple object copying. * **Cons:** May not handle complex nested objects as elegantly as `cloneDeep`, especially if you need to ensure immutability of the original object. **Other Considerations:** * **Performance:** The benchmark results show that the spread operator is significantly faster in this specific scenario. However, performance can vary depending on the complexity and size of the object being copied. * **Immutability:** Lodash's `cloneDeep` returns a completely new object, ensuring immutability of the original. The spread operator doesn't inherently guarantee immutability; you might still be modifying the original object if it is referenced elsewhere in your code. **Alternatives:** * **Object.assign():** A built-in JavaScript method that can copy properties from one object to another. It's generally faster than `cloneDeep` for simple cases, but may not handle nested objects as deeply. * **JSON Parsing/Serialization:** Converting the object to JSON and back can create a shallow copy. While straightforward, it might introduce performance overhead and loss of data if your object contains non-serializable properties (e.g., functions).
Related benchmarks:
Lodash cloneDeep VS spread operator
Lodash cloneDeep VS spread operator VS Lodash clone
Lodash clone VS Lodash cloneDeep VS Spread operator with array of objects
Lodash clone VS spread operator shallow
Spread Operator vs CloneDeep
Comments
Confirm delete:
Do you really want to delete benchmark?