Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array cloning 2023 (2)
(version: 0)
Comparing array cloning methods
Comparing performance of:
Slice vs Spread vs Map
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Slice
const arr = new Array(100000).fill(0).map(Math.random); arr.slice();
Spread
const arr = new Array(100000).fill(0).map(Math.random); [...arr];
Map
const arr = new Array(100000).fill(0).map(Math.random); const identity = a => a; arr.map(identity);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Slice
Spread
Map
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 benchmark measures the performance of three different methods for cloning an array in JavaScript: `slice()`, spread operator (`[...]`), and `map()`. In the test cases, a large array with 100,000 elements is created using `new Array(100000).fill(0).map(Math.random)`. Then, each method is used to create a new copy of this array. **Methods Comparison** 1. **Slice**: This method creates a shallow copy of the original array by calling `arr.slice()`. * Pros: + Simple and efficient. * Cons: + Only creates a copy of the array's elements, not its properties (e.g., `Object.prototype.toString()` would return `object Object`, not just the element value). 2. **Spread Operator**: This method uses the spread operator (`[...]`) to create a new array by spreading the elements of the original array. * Pros: + Creates a copy of the entire array, including its properties. + Generally faster than `slice()` for large arrays. 3. **Map**: This method creates a new array by calling `arr.map(identity)`, where `identity` is an anonymous function that returns each element as it is. * Pros: + Creates a copy of the entire array, including its properties. * Cons: + Generally slower than spread operator for large arrays. **Library and Special JS Features** None of the test cases use any external libraries. However, they do use some standard JavaScript features: 1. `Math.random()`: used to generate random numbers in each test case. 2. `const` keyword: used to declare constant variables (e.g., `const arr = ...;`, `const identity = a => a;`). 3. Arrow functions (`a => a`): used in the `Map` test case. **Other Alternatives** In addition to these three methods, there are other ways to clone an array in JavaScript: 1. **Using `Array.prototype.slice.call()`**: This method is similar to using the spread operator but may be slower for very large arrays. 2. **Using `Array.prototype.reduce()`**: This method can be used to create a new array by reducing the original array into smaller parts, but it's not typically faster than the three methods being compared here. Keep in mind that these alternative methods might have different performance characteristics depending on the specific use case and environment.
Related benchmarks:
JavaScript spread operator vs cloneDeep
Array cloning 2023
new Recursive Clone VS other Cloning Methods
montest
Comments
Confirm delete:
Do you really want to delete benchmark?