Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator magic
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.concat vs spread operator
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var base = [1 , 2] var params = [ "hello", true, 7 ]; var other = base.concat(params);
spread operator
var base = [1 , 2] var params = [ "hello", true, 7 ] var other = [ ...base, ...params ]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
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 provided benchmark and explain what's being tested. **What is tested?** The test compares two approaches for concatenating arrays in JavaScript: 1. The traditional `Array.prototype.concat()` method. 2. The new ES6 spread operator (`...`) for array concatenation. **Options compared:** * Traditional `concat()` method * Spread operator (`...`) **Pros and Cons of each approach:** * **Traditional `concat()` method:** + Pros: - Wide browser support (has been around since JavaScript 1.0) - Well-documented and widely understood + Cons: - Can be slower than the spread operator due to the overhead of calling a method on an array - Requires a separate array creation step, which can lead to increased memory usage * **Spread operator (`...`):** + Pros: - Faster execution time compared to `concat()` - More concise and expressive syntax + Cons: - Requires modern JavaScript engines (ES6+) for proper support - Can be less intuitive for developers who are not familiar with the spread operator **Library and its purpose:** None mentioned in the provided benchmark. However, it's worth noting that the `concat()` method is a part of the built-in `Array` prototype in JavaScript. **Special JS feature or syntax:** The spread operator (`...`) is a new feature introduced in ES6 (2015). It allows for concise array creation and expansion. **Other alternatives:** * **Array.prototype.push()**: While not exactly equivalent to concatenation, using `push()` can be an alternative approach. However, it requires creating a new array and then pushing each element individually. * **Array.prototype.slice()**: Another alternative is to use `slice()` to create a shallow copy of the original array and then append the new elements. **Benchmark preparation code and test cases:** The benchmark provides two test cases: 1. Using `concat()` method: ```javascript var base = [1, 2]; var params = ["hello", true, 7]; var other = base.concat(params); ``` 2. Using spread operator (`...`): ```javascript var base = [1, 2]; var params = ["hello", true, 7]; var other = [...base, ...params]; ``` **Benchmark result:** The latest benchmark result shows the execution performance for each test case: * `Array.prototype.concat()`: approximately 16.66 million executions per second * `spread operator (`...`): approximately 9.0125 million executions per second Keep in mind that these results may vary depending on the specific browser, device, and system configuration used for the benchmark.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (new try)
Array.prototype.concat vs spread operator (fix)
Array.prototype.concat vs spread operator on large array
Array.prototype.concat vs spread operator on small array
Comments
Confirm delete:
Do you really want to delete benchmark?