Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
(version: 0)
For times when a shallow copy is needed, what is the performance difference between these 3 methods.
Comparing performance of:
Lodash cloneDeep vs Lodash clone vs Array.slice() vs Object.assign()
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js'></script>
Script Preparation code:
var arr = ['one', 'two', 'three', {four: 'five'}];
Tests:
Lodash cloneDeep
var myCopy = _.cloneDeep(arr)
Lodash clone
var myCopy = _.clone(arr)
Array.slice()
var myCopy = arr.slice()
Object.assign()
var myCopy = Object.assign([], arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Lodash clone
Array.slice()
Object.assign()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
1534157.6 Ops/sec
Lodash clone
26218288.0 Ops/sec
Array.slice()
48397316.0 Ops/sec
Object.assign()
1233628.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is tested, the options compared, their pros and cons, and other considerations. **What is being tested?** The benchmark measures the performance difference between four methods to create a shallow copy of an array: Lodash `cloneDeep`, Lodash `clone`, Array `slice()`, and Object `assign()`. **Options compared:** 1. **Lodash `cloneDeep`**: Creates a deep copy of the original object, which means all nested properties are recursively cloned. 2. **Lodash `clone`**: Creates a shallow copy of the original object, which means only the top-level properties are copied and not any nested objects. 3. **Array `slice()`**: Returns a new array that contains a subset of elements from the original array, without modifying the original array. 4. **Object `assign()`**: Returns a new object that is a shallow copy of the original object, which means only the top-level properties are copied and not any nested objects. **Pros and Cons:** * **Lodash `cloneDeep`**: Pros: + Guarantees a deep copy of the original object. + Suitable for complex data structures with nested objects. Cons: + Can be slower than other methods due to its recursive cloning process. * **Lodash `clone`**: Pros: + Faster than `cloneDeep` since it only copies top-level properties. + Still a good choice when you need a shallow copy of an object. Cons: + May not work as expected if the original object has complex nested structures. * **Array `slice()`**: Pros: + Fast and efficient, especially for large arrays. + Does not modify the original array. Cons: + Only creates a shallow copy of the array's elements, not its nested objects. + May not work as expected if you need to preserve certain properties or data structures. * **Object `assign()`**: Pros: + Fast and efficient, especially for large objects. + Does not modify the original object. Cons: + Only creates a shallow copy of the top-level properties, not any nested objects. **Library used:** The benchmark uses Lodash (a popular JavaScript utility library) for its `cloneDeep` and `clone` functions. The `assign` method is built-in to JavaScript. **Special JS features or syntax:** None mentioned in the provided JSON, but it's worth noting that Lodash `cloneDeep` uses a recursive function to clone objects, which might be slower than other methods for very large data structures. **Alternatives:** Other alternatives to create a shallow copy of an array or object include: * Using the spread operator (`...`) in JavaScript (available from ES6 onwards): `const myCopy = [...arr];` or `const myCopy = { ...obj };` * Utilizing libraries like Immutable.js, which provides a set of immutable data structures and utilities. * Implementing custom cloning logic using JavaScript's built-in methods, such as `JSON.parse(JSON.stringify(obj))`. Keep in mind that the performance differences between these alternatives may vary depending on the specific use case and data structure.
Related benchmarks:
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)
Array.slice() vs. Spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?