Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test spread vs foreach array
(version: 0)
Comparing performance of:
array spread vs forEach
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = ['a', 'b', 'c']; var arr2 = ['aa', 'bb', 'cc']; var arr3 = ['aaa', 'bbb', 'ccc']; var arr4 = ['aaaa', 'bbbb', 'cccc']; var arr5 = ['aaaaa', 'bbbbb', 'ccccc'];
Tests:
array spread
var result = [...arr1, ...arr2, ...arr3, ...arr4, ...arr5];
forEach
var result = []; arr1.forEach(r => result.push(r)) arr2.forEach(r => result.push(r)) arr3.forEach(r => result.push(r)) arr4.forEach(r => result.push(r)) arr5.forEach(r => result.push(r))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array spread
forEach
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 provided benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Definition** The benchmark is comparing two ways to concatenate arrays in JavaScript: using the spread operator (`...`) and `forEach` loop with `push`. **Options Compared** 1. **Spread Operator**: Using the spread operator (`...`) to concatenate arrays. 2. **ForEach Loop with Push**: Using a `forEach` loop with `push` to concatenate arrays. **Pros and Cons of Each Approach** **Spread Operator** Pros: * More concise and readable code * Fast, as it creates a new array without mutating the original one * Works well with modern JavaScript engines Cons: * Requires support for modern JavaScript features (ECMAScript 2018 and above) * May not work in older browsers or environments **ForEach Loop with Push** Pros: * Works in all environments that support JavaScript, including older browsers * Can be useful when working with large datasets, as it avoids creating a new array Cons: * Less concise and readable code compared to the spread operator * Slower, as it mutates the original array and creates multiple function calls * May lead to performance issues if not optimized correctly **Library/ Framework** None in this specific benchmark. The only library used is `var`, which is a built-in JavaScript keyword. **Special JS Feature/Syntax** The spread operator (`...`) was introduced in ECMAScript 2018 and is supported by most modern JavaScript engines. It's a new feature that allows for more concise array creation. Other Alternatives For concatenating arrays, other alternatives include: * Using the `concat()` method: `arr1.concat(arr2)` * Using the `reduce()` method: `arr1.reduce((a, b) => a.concat(b))` * Using a loop with `+=` operator (not recommended, as it's slow and can lead to performance issues) In conclusion, the benchmark is testing two common ways to concatenate arrays in JavaScript, comparing their performance and readability. The spread operator is generally considered more concise and fast, but may require support for modern features. The `forEach` loop with `push` is slower and less readable, but works in all environments that support JavaScript.
Related benchmarks:
Test concat vs spread operator
Array concat vs. spread operator
Array.from vs Array spread with mapping of values
Array.from vs Spread on arrays
Large Array: concat vs spread vs push
Comments
Confirm delete:
Do you really want to delete benchmark?