Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Adam -- Array concat vs spread operator vs push
(version: 1)
Compare the new ES6 spread operator with the traditional concat() method and push
Comparing performance of:
Array.prototype.concat vs spread operator vs directly push
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var jobs = [1, 1, 1]; var allJobs = [];
Tests:
Array.prototype.concat
for(i=0; i<100; i++){allJobs.concat(jobs);}
spread operator
for(i=0; i<100; i++){allJobs = [ ...jobs, ...allJobs ];}
directly push
for(i=0; i<100; i++){jobs.forEach(i => allJobs.push(i));}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
directly push
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three different approaches to concatenate an array in JavaScript: 1. `Array.prototype.concat()` 2. The new ES6 spread operator (`[ ...jobs, ...allJobs ]`) 3. Directly pushing elements onto the `allJobs` array using a `forEach` loop. **Options Compared** The benchmark is comparing two main options for concatenating arrays: * Traditional `concat()` method * New ES6 spread operator Additionally, there's a third option being tested, which might seem less optimal at first glance: directly pushing elements onto the `allJobs` array using a `forEach` loop. **Pros and Cons of Each Approach** 1. **Array.prototype.concat()** * Pros: + Widely supported across browsers + Well-documented and easy to understand * Cons: + Can be slower than the spread operator due to its iterative nature 2. **ES6 Spread Operator ([ ...jobs, ...allJobs ] )** * Pros: + Faster than `concat()` due to its ability to create a new array in one operation + More concise and readable code * Cons: + May not be supported by older browsers (though this is unlikely) 3. **Directly Pushing Elements onto the Array** * Pros: + Can be optimized for specific use cases where `concat()` or spread operator are too expensive * Cons: + Less readable and less efficient than the other two options **Library Used** In none of the benchmark test cases is a library used. The code is pure JavaScript, which means it can run on any device that supports JavaScript without relying on external dependencies. **Special JS Features or Syntax** The ES6 spread operator (`[ ...jobs, ...allJobs ]`) uses a new syntax introduced in ECMAScript 2018 (ES8). It's a feature that allows creating new arrays by spreading elements from existing arrays. **Alternative Approaches** Other approaches to concatenate arrays might include: * `Array.prototype.push.apply()`: This method pushes all elements of an array onto the end of another array, which can be faster than concatenating individual elements. * Custom looping: Using a traditional loop (e.g., `for` or `while`) to iterate over each element and push it onto the `allJobs` array.
Related benchmarks:
Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (fix)
Array.prototype.concat vs spread operator on large array
Comments
Confirm delete:
Do you really want to delete benchmark?