Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread
(version: 0)
Comparing performance of:
Spread vs Concat
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = (new Array(100)).fill(0).map((x, i) => i) var arr2 = (new Array(100)).fill(0).map((x, i) => (i + 1) * 2)
Tests:
Spread
const arr = [...arr1, ...arr2]
Concat
const arr = arr1.concat(arr2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread
Concat
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to concatenating arrays in JavaScript: using the spread operator (`...`) and using the `concat()` method. **Script Preparation Code** The script preparation code generates two arrays, `arr1` and `arr2`, each with 100 elements. The elements are integers starting from 0 and incrementing by 1 for `arr1`, and by 2 for `arr2`. These arrays will be used as input to the benchmark. **Html Preparation Code** There is no HTML preparation code provided, which means that only JavaScript execution time is being measured. **Test Cases** The benchmark consists of two test cases: 1. **Spread**: The first test case uses the spread operator (`...`) to concatenate `arr1` and `arr2`. This approach creates a new array with all elements from both input arrays. 2. **Concat**: The second test case uses the `concat()` method to concatenate `arr1` and `arr2`. This approach returns a new array with all elements from both input arrays. **Library and Purpose** Neither of the libraries mentioned in the benchmark definition are explicitly stated as being used, but it's likely that the `Array.prototype.concat()` method is being compared. The purpose of this method is to concatenate two or more arrays into a single array. **Special JS Features/Syntax** The benchmark uses the spread operator (`...`), which is a relatively modern JavaScript feature introduced in ECMAScript 2015 (ES6). This feature allows for concise array creation and expansion. In this benchmark, it's used to create a new array with all elements from both input arrays. **Pros and Cons of Approaches** 1. **Spread Operator (`...`)**: * Pros: Concise syntax, can be more expressive, and may be faster in some cases due to its avoidance of creating intermediate objects. * Cons: May not work as expected if `null` or `undefined` is passed as an argument (although this is unlikely for arrays). 2. **Concat Method (`concat()`)**: * Pros: Widely supported across browsers and Node.js versions, simple syntax, and works with any array-like object. * Cons: May create intermediate objects, which can be slower than the spread operator. **Other Alternatives** If you need to concatenate arrays in JavaScript, there are other approaches: 1. **Array.prototype.push()**: You can push all elements from one or more arrays into another array using `push()`. This approach is similar to concatenating but creates a new array instead. 2. **String Concatenation**: You can also use string concatenation to concatenate arrays, although this approach is less common and may not be suitable for large datasets. In summary, the benchmark compares two approaches to concatenating arrays in JavaScript: using the spread operator (`...`) and using the `concat()` method. The spread operator provides a concise syntax but may not work as expected with certain input types. The `concat()` method is widely supported but may create intermediate objects, making it slower than the spread operator in some cases.
Related benchmarks:
Array push vs spread vs concat
Array concat vs. spread operator
Array merge (spread, concat)
Splice vs Spread vs Unshift vs Concat to insert at beginning of array (fixed from slice)
Comments
Confirm delete:
Do you really want to delete benchmark?