Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs forEach push v4
(version: 0)
Comparing performance of:
spread vs forEach push
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from(Array(1000).keys())
Tests:
spread
var brr = []; brr.push(...arr);
forEach push
var drr = []; arr.forEach(item => { drr.push(item); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
forEach push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 123 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread
396459.0 Ops/sec
forEach push
215115.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and test cases for you. **Benchmark Definition:** The benchmark definition is a JSON object that describes two different approaches to adding elements to an array: 1. `var brr = [];` (initializing an empty array) 2. `arr.forEach(item => {\r\n\tdrr.push(item);\r\n});` (using the `forEach` method) The purpose of this benchmark is to compare the performance of these two approaches when adding elements to an array. **Options Compared:** Two options are compared: 1. **Spread Operator (`...`)**: used in the line `brr.push(...arr);` 2. **forEach Method**: used in the line `arr.forEach(item => {\r\n\tdrr.push(item);\r\n});` **Pros and Cons of Each Approach:** 1. **Spread Operator (`...`)**: * Pros: + Can be more readable and concise, especially for small arrays. + Allows for easy iteration over arrays using `for...of`. * Cons: + May have performance overhead due to the need to create a new array. 2. **forEach Method**: * Pros: + Can be used with other methods that modify the array, such as `map` and `filter`. + Allows for callback functions to perform custom operations on each element. * Cons: + May have performance overhead due to the need to iterate over the entire array. **Library/Functionality Used:** None explicitly mentioned in this benchmark. However, `Array.from()` is used to create an initial array, which is then passed as an argument to the spread operator or used with `forEach`. **Special JS Feature/Syntax:** * **Spread Operator (`...`)**: Introduced in ECMAScript 2015 (ES6), it allows for element-wise expansion of arrays. * **forEach Method**: Introduced in ECMAScript 1999 (ECMAScript 3), it allows iteration over arrays using a callback function. **Other Alternatives:** If you want to explore other approaches, here are some alternatives: 1. `arr.concat()`: Concatenates the array with the elements from the provided iterable. 2. `Array.prototype.push.apply(arr, arr)`: A more explicit way of pushing all elements from an array onto another array. 3. `for...of loop` without using `forEach`: You can use a traditional `for` loop to iterate over arrays. Here's an example implementation: ```javascript let brr = []; for (let item of arr) { brr.push(item); } ``` Keep in mind that the performance differences between these approaches may not be significant for small arrays, but they can become noticeable as the array size increases.
Related benchmarks:
Arrays: spread operator vs push
Array: spread operator vs push
Push vs Spread JavaScript
spread vs forEach push v3
Comments
Confirm delete:
Do you really want to delete benchmark?