Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
concat vs push in various ways
(version: 0)
Comparing performance of:
concat vs push with rest params vs push in for loop vs push with forEach
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
concat
let x = []; let y = [1, 2, 3, 4, 5, 6, 7]; for (let ii = 0; ii < 1000; ii++) { x = x.concat(y); }
push with rest params
let x = []; let y = [1, 2, 3, 4, 5, 6, 7]; for (let ii = 0; ii < 1000; ii++) { x.push(...y); }
push in for loop
let x = []; let y = [1, 2, 3, 4, 5, 6, 7]; for (let ii = 0; ii < 1000; ii++) { for (let item of y) { x.push(item); } }
push with forEach
let x = []; let y = [1, 2, 3, 4, 5, 6, 7]; for (let ii = 0; ii < 1000; ii++) { y.forEach(num => x.push(num)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
concat
push with rest params
push in for loop
push with 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 break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares different approaches for adding elements to an array in JavaScript: 1. `concat` 2. `push` with rest parameters (`...`) 3. `push` inside a `for` loop 4. `push` with `forEach` **Library Usage** None of the test cases explicitly use any external libraries. **Special JS Features/Syntax** The benchmark uses several special JavaScript features: 1. **Rest Parameters (`...`)**: Introduced in ECMAScript 2015, rest parameters allow a function to accept an arbitrary number of arguments, which can be accessed as an array. 2. `for...of` loop with `push`: This is a relatively recent feature introduced in ECMAScript 2017. 3. `forEach()` method: This is a built-in JavaScript method for iterating over arrays. **Options Being Compared** The benchmark compares the performance of each approach: 1. `concat`: Using the `concat()` method to add elements to an array. 2. `push` with rest parameters (`...`): Using the spread operator (`...`) to add all elements from another array to a target array using `push()`. 3. `push` inside a `for` loop: Iterating over each element of an array and adding it to the target array using `push()`. 4. `push` with `forEach()` method: Using `forEach()` to iterate over each element of an array and add it to the target array using `push()`. **Pros and Cons of Each Approach** Here's a brief summary: 1. **concat**: * Pros: Simple, straightforward approach. * Cons: Can be slow for large arrays due to the creation of a new array. 2. **Push with rest parameters (`...`)**: * Pros: Efficient way to add all elements from another array using `push()`. * Cons: Requires support for rest parameters in the browser being tested (Chrome 86). 3. **Push inside a `for` loop**: * Pros: Allows control over iteration, but can be slower due to the overhead of the loop. * Cons: Less readable and less efficient than using `forEach()` or `push` with rest parameters. 4. **Push with `forEach()` method**: * Pros: More readable than a custom loop, and efficient for large arrays. * Cons: Requires support for the `forEach()` method in the browser being tested (Chrome 86). **Other Considerations** 1. **Array Iteration**: The benchmark assumes that iterating over an array using `for...of` loops or `forEach()` is more efficient than a custom loop. This is generally true, but the performance difference may be negligible for small arrays. 2. **Browser Support**: The benchmark uses Chrome 86 as the test browser, which means that rest parameters and `forEach()` method are required for the benchmarks to work. **Alternatives** Other approaches for adding elements to an array in JavaScript include: 1. Using `Array.prototype.push.apply()` 2. Using a `for` loop with indexing (e.g., `x[x.length++] = y[i];`) 3. Using a custom loop with array methods like `splice()` or `unshift()` These alternatives may offer different performance profiles compared to the approaches tested in this benchmark.
Related benchmarks:
Array push vs spread vs concat
Array concat vs spread operator vs push (1)
concat vs spread vs push vs push fn
Array concat vs spread operator vs push for single values
array spread operator vs concat vs push fix
Comments
Confirm delete:
Do you really want to delete benchmark?