Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread vs push operator - large array 100000
(version: 0)
Compare the new ES6 spread operator with the traditional slice() method
Comparing performance of:
Array.prototype.slice vs spread operator vs Push operation
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({length: 10000}, () => Math.random())
Tests:
Array.prototype.slice
var other = arr.slice();
spread operator
var other = [ ...arr ]
Push operation
var other = [] arr.forEach(el => other.push(el))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.slice
spread operator
Push 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):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided JSON represents a benchmark test case that compares three different approaches for creating a copy of an array: `Array.prototype.slice()`, the ES6 spread operator (`[ ...arr ]`), and the `push()` method with a loop (`arr.forEach(el => other.push(el))`). The test is focused on performance, specifically measuring how many executions per second each approach can handle a large array of 100,000 elements. **Options compared:** 1. **Array.prototype.slice()**: This traditional method creates a shallow copy of an array by returning a new array object with references to the original elements. 2. **ES6 spread operator (`[ ...arr ]`)**: This operator creates a new array with all the elements of the original array, creating a deep copy. 3. **Push operation with a loop (`var other = []\r\narr.forEach(el => other.push(el))`)**: This approach creates a new array by pushing each element of the original array into it. **Pros and cons of each approach:** 1. **Array.prototype.slice()**: * Pros: + Fast, as it only requires a single loop through the array. + Suitable for large arrays, as it uses caching to improve performance. * Cons: + Creates a shallow copy, which may not be suitable for all use cases (e.g., objects with nested references). 2. **ES6 spread operator (`[ ...arr ]`)**: * Pros: + Creates a deep copy, ensuring that original array modifications do not affect the copied array. * Cons: + May be slower than `slice()` for very large arrays due to the additional overhead of creating an iterator and iterating over the elements. 3. **Push operation with a loop (`var other = []\r\narr.forEach(el => other.push(el))`)**: * Pros: + Creates a deep copy, ensuring that original array modifications do not affect the copied array. * Cons: + Generally slower than `slice()` due to the additional overhead of the loop and push operations. **Library used:** None explicitly mentioned in this test case. However, it's likely that the benchmark uses internal JavaScript functions or libraries like V8 (the engine behind Google Chrome) to execute the code. **Special JS feature/syntax:** No special features or syntax are highlighted in this test case. **Other alternatives:** For creating copies of arrays, other methods exist: 1. `Array.from()`: Creates a new array from an iterable source (like another array). This method is often used for creating large arrays and can be faster than `slice()` for very large arrays. 2. `Array.prototype.map()`: Creates a new array by calling a provided function on each element of the original array. While not ideal for copying, it can be used in combination with other methods to achieve similar results. In summary, this benchmark test case provides a useful comparison of three different approaches for creating an array copy: traditional `slice()`, ES6 spread operator, and push operation with a loop. The choice of method depends on the specific requirements and performance characteristics of your use case.
Related benchmarks:
Array.prototype.slice vs spread operator with length limit
Array.prototype.slice vs spread operator on a bigger array
Array.prototype.slice vs spread operator - large array 100000
Array.prototype.slice vs spread operator performance
Comments
Confirm delete:
Do you really want to delete benchmark?