Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
concatenating 2 arrays
(version: 0)
Comparing performance of:
push apply vs push spread vs concat vs for push vs for of push vs forEach push
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
push apply
var arr = [1,2,3,4,5,6,7,8,9,10] var arr2 = [] Array.prototype.push.apply(arr2, arr)
push spread
var arr = [1,2,3,4,5,6,7,8,9,10] var arr2 = [] arr2.push(...arr)
concat
var arr = [1,2,3,4,5,6,7,8,9,10] var arr2 = [] arr2 = arr2.concat(arr)
for push
var arr = [1,2,3,4,5,6,7,8,9,10] var arr2 = [] for(var i = 0, len = arr.length; i < len; i++) { arr2.push(arr[i]) }
for of push
var arr = [1,2,3,4,5,6,7,8,9,10] var arr2 = [] for(var item of arr) { arr2.push(item) }
forEach push
var arr = [1,2,3,4,5,6,7,8,9,10] var arr2 = [] arr.forEach(function(item) { arr2.push(item) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
push apply
push spread
concat
for push
for of push
forEach 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):
I'd be happy to explain what's being tested in the provided JSON. **Benchmark Definition** The benchmark definition represents a JavaScript operation that concatenates two arrays. The operation is defined as follows: * Two arrays, `arr` and `arr2`, are created with different methods: + Some tests use `Array.prototype.push.apply(arr2, arr)`. + Some tests use the spread operator (`...`) to concatenate the arrays: `arr2.push(...arr)`. + Some tests use the `concat()` method to concatenate the arrays: `arr2 = arr2.concat(arr)`. + Two tests use a `for` loop with array iteration: `for(var i = 0, len = arr.length; i < len; i++) {arr2.push(arr[i])}` and `for(var item of arr) {arr2.push(item)}`. * The test measures the execution time for each method. **Options Compared** The benchmark compares four different methods to concatenate two arrays: 1. **`Array.prototype.push.apply()`**: This method applies an array function to all elements of the original array, in this case, `push()` to the second array. 2. **Spread operator (`...`)**: This method uses the spread operator to expand the first array and push its elements onto the second array. 3. **`concat()` method**: This method creates a new array by concatenating the two input arrays. 4. **`for` loop with array iteration**: Two tests use a `for` loop to iterate over each element of the first array and push it onto the second array. **Pros and Cons** Each method has its own trade-offs: * **`Array.prototype.push.apply()`**: Fast, but can be slower for large arrays due to the overhead of function application. * **Spread operator (`...`)**: Concise and expressive, but may incur a small performance penalty due to the creation of temporary arrays. * **`concat()` method**: Straightforward and easy to read, but creates a new array, which can lead to increased memory usage. * **`for` loop with array iteration**: Control over the iteration process, but can be slower and more verbose than other methods. **Other Considerations** * The benchmark assumes that the input arrays have the same length. If the arrays are of different lengths, the results may vary depending on which method is used. * The benchmark does not account for potential side effects or modifications to the original arrays during the concatenation process. **Alternative Approaches** Other alternatives to concatenate two arrays in JavaScript include: * Using `Array.prototype.reduce()` with an accumulator function * Using a library like Lodash's `concat` function * Implementing custom concatenation functions, such as using a linked list or other data structure Keep in mind that these alternative approaches may have different performance characteristics and trade-offs compared to the methods tested in this benchmark.
Related benchmarks:
spread vs concat vs unshift (2)
spread vs concat vs unshift 213124
spread vs concat vs unshift for arrays
Splice+Spread vs concat to concat arrays
JavaScript spread operator vs Array.concat performance
Comments
Confirm delete:
Do you really want to delete benchmark?