Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare spread operator and concat in arrays
(version: 5)
This benchmark is to compare the cost of using the spread operator
Comparing performance of:
Using the spread operator vs Using Array.prototype.concat
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array1 = []; var array2 = []; for (let i = 0; i < 1000; i++) { array1[i] = Math.floor(Math.random() * 100); array2[i] = Math.floor(Math.random() * 100); }
Tests:
Using the spread operator
const array3 = [...array1, ...array2];
Using Array.prototype.concat
const array3 = array1.concat(array2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Array.prototype.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 what's being tested in this benchmark and analyze the options compared. **Benchmark Description** The benchmark is designed to compare the performance of two ways to concatenate arrays: using the spread operator (`...`) and `Array.prototype.concat()`. **Options Compared** 1. **Using Spread Operator**: `const array3 = [...array1, ...array2];` * Pros: + More concise and readable code. + Can be faster for large arrays since it avoids creating an intermediate array. * Cons: + Requires support for the spread operator in older browsers (pre-ES6). + May have performance advantages in newer browsers with optimized spread operator implementation. 2. **Using Array.prototype.concat()**: `const array3 = array1.concat(array2);` * Pros: + Wider browser support (works in all browsers that support JavaScript). + Can be easier to understand for developers familiar with the `concat()` method. **Performance Considerations** The benchmark is designed to measure the execution time of each approach. In general, using the spread operator can lead to better performance for large arrays since it avoids creating an intermediate array. However, the actual performance difference may depend on various factors such as: * Array size and distribution * Browser support and optimization level * JavaScript engine used (e.g., V8 in Chrome) * CPU architecture **Library Used** The benchmark uses `Math.random()` to generate random numbers for populating the arrays. **Special JS Feature or Syntax** There is no specific use of special JavaScript features or syntax in this benchmark. Both approaches are relatively straightforward and don't rely on advanced ES6+ features like `let` or `const`. **Alternatives** Other alternatives to compare array concatenation methods could include: * Using `Array.prototype.push()` with multiple arguments * Using a library like Lodash's `concat` function * Comparing the performance of custom implementations using different data structures (e.g., linked lists) Keep in mind that these alternative approaches might not accurately represent real-world use cases and are intended to provide additional context for understanding the benchmark results.
Related benchmarks:
Array.prototype.concat vs spread operator
Array concat vs spread operator [2]
JavaScript spread operator vs Array.concat performance
Array.prototype.concat vs spread operator - large arrays
Comments
Confirm delete:
Do you really want to delete benchmark?