Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs concat vs apply on large data set - 2
(version: 1)
Large data set is the difference
Comparing performance of:
array spread vs array concat vs array push apply vs array spread in a push
Created:
7 years ago
by:
Registered User
Jump to the latest result
Tests:
array spread
const arr = [] for(i=0;i<10000;i++){ arr.push(i); } const arr2 = [1,2]; const newArray = [1,2,...arr]
array concat
const arr = [] for(i=0;i<10000;i++){ arr.push(i); } const arr2 = [1,2]; const newArray = arr2.concat(arr)
array push apply
const arr = [] for(i=0;i<10000;i++){ arr.push(i); } const arr2 = [1,2]; const newArray = arr2.push.apply(arr2, arr)
array spread in a push
const arr = [] for(i=0;i<10000;i++){ arr.push(i); } const arr2 = [1,2]; const newArray = arr2.push(...arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array spread
array concat
array push apply
array spread in a 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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of three different ways to combine arrays in JavaScript: the spread operator (`...`), the `concat()` method, and the `apply()` method with the `push()` function. **Here's a breakdown:** * **Test Cases:** * **`array spread`:** This test uses the spread operator (`...`) to combine the elements of a large array (`arr`) into another array (`arr2`). * **`array concat`:** This test uses the `concat()` method to combine the elements of `arr` into `arr2`. * **`array push apply`:** This test uses the `apply()` method with the `push()` function to add all the elements of `arr` to the end of `arr2`. * **`array spread in a push`:** This test combines the spread operator and `push()`. It spreads the elements of `arr` into `arr2`, effectively appending them. **Pros and Cons:** * **Spread Operator (`...`)**: * **Pros:** Generally considered the most concise and readable option, especially for simple array combinations. JavaScript optimizes it well. * **Cons:** Might not be as performant for very large arrays compared to `concat()` when used repeatedly. * **`concat()` Method:** * **Pros:** Can be efficient for combining multiple arrays, potentially better performance than the spread operator for larger datasets. * **Cons:** Can create a new array object with each call. * **`push.apply()`**: * **Pros:** Often used when you need to modify an existing array in place. It can be faster than `concat()` for very large arrays. * **Cons:** More complex syntax, less readable than the spread operator or `concat()`. **Other Considerations:** * **Array Size:** The best method might depend on the size of your arrays. For small arrays, the differences in performance are likely negligible. For extremely large arrays, benchmarking with specific data sets is crucial. * **Memory Usage:** While `concat()` creates a new array, the spread operator and `push.apply()` usually have less impact on memory usage. * **Code Readability:** The spread operator (`...`) often leads to more readable code, making it a preferred choice in many scenarios. **Alternatives:** * Libraries like Lodash or Underscore provide optimized versions of array methods like `concat()`, which can sometimes offer better performance. However, these libraries add extra dependencies to your project.
Related benchmarks:
Array.prototype.concat vs Spread operator
Array.prototype.concat vs Spread operator
Concat vs Spread for Large Arrayss
spread vs concat vs unshift to join arrays
Array.prototype.concat vs spread operator [huge collection] 2
Comments
Confirm delete:
Do you really want to delete benchmark?