Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
merge arrays
(version: 0)
Comparing performance of:
spread syntax vs concat vs push
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
spread syntax
for(let i = 10000; i >0; i--){ // Accept indefinite number of arrays as param, return one merged array function mergeArrays(...arrays) { let jointArray = []; arrays.forEach((array) => { jointArray = [...jointArray, ...array]; }); return jointArray; } console.log(mergeArrays([1, 2], [3, 4], [1, 5], [4, 5, 6])); }
concat
for(let i = 10000; i >0; i--){ // Accept indefinite number of arrays as param, return one merged array function mergeArrays(...arrays) { let jointArray = []; arrays.forEach((subArr) => { jointArray = jointArray.concat(subArr); }); return jointArray; } console.log(mergeArrays([1, 2], [3, 4], [1, 5], [4, 5, 6])); }
push
for(let i = 10000; i >0; i--){ // Accept indefinite number of arrays as param, return one merged array function mergeArrays(...arrays) { let jointArray = []; arrays.forEach((subArr) => { jointArray.push(...subArr); }); return jointArray; } console.log(mergeArrays([1, 2], [3, 4], [1, 5], [4, 5, 6])); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread syntax
concat
push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread syntax
40.3 Ops/sec
concat
39.4 Ops/sec
push
41.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Purpose** The purpose of this benchmark is to compare the performance of three different ways to merge arrays in JavaScript: using the `push` method, the spread syntax (`...`), and the `concat` method. **Options Compared** 1. **Push**: This method uses the `push` method to add elements to an array. 2. **Spread Syntax ( ... )**: This method uses the spread operator to merge arrays into a new array. 3. **Concat**: This method uses the `concat` method to merge arrays into a new array. **Pros and Cons of Each Approach** 1. **Push**: * Pros: Fast, efficient, and widely supported. * Cons: Can lead to inefficient memory allocation if not used carefully (e.g., when dealing with large arrays). 2. **Spread Syntax ( ... )**: * Pros: Modern, concise, and easy to read. * Cons: May have performance overhead due to the creation of a new array, and may not work as expected in older browsers or environments. 3. **Concat**: * Pros: Can be faster than push for large arrays, but can lead to inefficient memory allocation if not used carefully. * Cons: Less modern and less concise than spread syntax. **Library Used** There is no explicit library mentioned in the benchmark definition or test cases. However, it's likely that the browser being tested (Chrome 108) has built-in support for these array merge methods. **Special JavaScript Features/Syntax** The spread syntax (`...`) is a modern JavaScript feature introduced in ECMAScript 2018. It allows arrays to be merged into a new array using the `...` operator. **Other Considerations** * The benchmark only tests merging arrays, not other operations like sorting or reversing. * The test cases use an indefinite number of arrays as input, which may not reflect real-world scenarios where arrays typically have a fixed size. * The benchmark does not account for edge cases like empty arrays or null inputs. **Alternatives** Other alternatives for merging arrays in JavaScript include: 1. **Array.prototype.push.apply()**: This method applies the `push` method to an array of elements, which can be more efficient than pushing each element individually. 2. **Array.prototype.reduce()**: This method reduces an array to a single value by applying a callback function to each element. However, these alternatives are not tested in this benchmark.
Related benchmarks:
Concat VS Spread operator benchmark
Sorting test
spread/concat large
Merge 3 small arrays
Merging two arrays
Comments
Confirm delete:
Do you really want to delete benchmark?