<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js'></script>
var arr = ['one', 'two', 'three', {four: 'five'}];
var myCopy = _.cloneDeep(arr)
var myCopy = _.clone(arr)
var myCopy = arr.splice(0, arr.length)
var myCopy = Object.assign([], arr)
var myCopy = arr.slice()
var myCopy = arr.slice(0)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Lodash clone | |
Array.splice() | |
Object.assign() | |
Array,slice() | |
Array.slice(0) |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 1760900.1 Ops/sec |
Lodash clone | 7865186.5 Ops/sec |
Array.splice() | 11390228.0 Ops/sec |
Object.assign() | 8624743.0 Ops/sec |
Array,slice() | 24762980.0 Ops/sec |
Array.slice(0) | 24748952.0 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to compare the performance of different methods for various tasks.
The provided benchmark measures the performance difference between six methods for creating a shallow copy of an array:
cloneDeep
clone
Array.splice()
Object.assign()
Array.slice()
Array.slice(0)
Method Comparison
Here's a brief explanation of each method and their pros/cons:
cloneDeep
Lodash's cloneDeep
is a deep copy function that recursively clones the entire object, including nested arrays and objects.
Pros: Creates an exact replica of the original object, preserves nested data structures. Cons: Can be slow for large objects due to recursive cloning.
Use case: When you need to create an exact replica of an object with complex nested structures.
clone
Lodash's clone
is a shallow copy function that creates a new array or object by copying the top-level properties of the original.
Pros: Fast and efficient, suitable for most use cases. Cons: Does not preserve nested data structures.
Use case: When you need to create a quick copy of an object with simple top-level properties.
Array.splice()
Array.splice()
modifies the original array by removing elements from the start or end and returning an array of removed elements.
Pros: Can be used for various tasks beyond just creating a copy. Cons: Modifies the original array, not suitable for all use cases.
Use case: When you need to perform an in-place operation on an array.
Object.assign()
Object.assign()
creates a new object by copying all enumerable own properties from one or more source objects.
Pros: Fast and efficient, suitable for most use cases. Cons: Does not preserve nested data structures.
Use case: When you need to create a quick copy of an object with simple top-level properties.
Array.slice()
Array.slice()
creates a shallow copy of a subset of elements from the end of an array, starting at the specified index.
Pros: Fast and efficient, suitable for most use cases. Cons: Does not preserve nested data structures.
Use case: When you need to create a quick copy of a portion of an array.
Array.slice(0)
Array.slice(0)
creates a shallow copy of the entire array, starting from the beginning and ending at index 0.
Pros: Fast and efficient, suitable for most use cases. Cons: Does not preserve nested data structures.
Use case: When you need to create an exact replica of an array with no modifications.
Library
Lodash is a popular JavaScript library that provides various utility functions, including cloneDeep
and clone
.
Special JS Feature/Syntax
There are no special JavaScript features or syntax used in this benchmark.
Other Alternatives
For creating shallow copies of objects and arrays, other alternatives to Lodash include:
Array.prototype.slice()
or Object.assign()
)For deep cloning objects with complex nested structures, other alternatives to Lodash's cloneDeep
include: