Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Combining Arrays: Spread vs. Concat
(version: 0)
Comparing performance of:
Spread vs Concat
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = []; for (var j; j < 300; j++) { arr1.push(Math.random().toString(16).substr(2, 7)); } var arr2 = []; for (var j; j < 230; j++) { arr2.push(Math.random().toString(16).substr(2, 9)); } var arr3 = []; for (var j; j < 80; j++) { arr3.push(Math.random().toString(16).substr(2, 8)); } var arr4 = []; for (var j; j < 450; j++) { arr4.push(Math.random().toString(16).substr(2, 6)); } console.log("Creating arrays:", arr1, arr2, arr3, arr4);
Tests:
Spread
var bigArraySpread = [...arr1, ...arr2, ...arr3, ...arr4];
Concat
var bigArrayConcat = [].concat(arr1, arr2, arr3, arr4);
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):
**Benchmark Explanation** The provided benchmark measures the performance difference between two methods of combining arrays in JavaScript: using the spread operator (`...`) and concatenation with `Array.prototype.concat()`. The test cases are designed to simulate real-world scenarios where large arrays need to be combined. **Options Compared** There are two main options compared: 1. **Spread Operator**: Using the spread operator (`...`) to combine arrays. 2. **Concatenation**: Using `Array.prototype.concat()` to concatenate arrays. **Pros and Cons of Each Approach** * **Spread Operator**: + Pros: - More concise and readable syntax. - Creates a new array with the elements from the original arrays, which can be more efficient for large datasets. + Cons: - May not work as expected if the arrays contain non-array values (e.g., objects or strings). * **Concatenation**: + Pros: - More flexible and forgiving than the spread operator, as it works with non-array values. + Cons: - Less concise and less readable syntax. - Creates a new array with the elements from the original arrays, which can lead to performance issues for large datasets. **Library Used** There is no external library used in this benchmark. The test code only uses built-in JavaScript features. **Special JS Feature or Syntax** The benchmark takes advantage of modern JavaScript features, specifically: * **Destructuring assignment**: Used in the `for` loops to initialize variables. * **Rest parameter syntax**: Used with `Math.random().toString(16).substr(2, x)` to generate random strings. * **Arrow functions**: Not used explicitly in this benchmark, but implied by the use of concise syntax. **Other Considerations** To provide accurate results, the benchmark: * Uses a consistent number of iterations for each array (300, 230, 80, and 450). * Generates random data to simulate real-world scenarios. * Compares the performance of both methods using the same codebase. **Alternative Benchmarks** If you'd like to explore alternative benchmarks or modifications to this one, consider the following: * **Other array combination methods**: Benchmarking `Array.prototype.push()` and `at()` would provide additional insights into array manipulation performance. * **Data structure variations**: Comparing the performance of arrays with other data structures (e.g., objects, sets) could reveal interesting results. * **Edge cases**: Investigating specific edge cases, such as combining very large or empty arrays, could help identify bottlenecks in these methods.
Related benchmarks:
Array.concat vs Array.prototype.concat.apply
Combining Arrays: Spread vs. Concat 2
concat.apply short form new array vs flat
Comparing array.concat.apply short form with empty array vs array.flat vs array.reduce
Comments
Confirm delete:
Do you really want to delete benchmark?