Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
concat vs spread three arrays
(version: 0)
Compare the spread operator with the concat() method
Comparing performance of:
concat vs spread
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5]; var b = [6, 7, 8, 9, 10, 11]; var c= [12, 13, 14, 15, 16];
Tests:
concat
var res = [].concat(a, b, c);
spread
var res = [...a, ...b, ...c];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
concat
spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
concat
5467685.5 Ops/sec
spread
7476074.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and discussed in this JavaScript microbenchmark. **What is being tested?** The benchmark compares two approaches to concatenate three arrays: using the `concat()` method versus the spread operator (`...`). **Options compared:** 1. **Concatenation using `concat()`**: This method creates a new array by concatenating the input arrays. 2. **Spread operator (`...`)**: This syntax allows you to "spread" the elements of an iterable (like an array) into another iterable. **Pros and Cons:** * **Concatenation using `concat()`:** + Pros: - Widespread support in older browsers (pre-ES6). - Easy to read and understand. + Cons: - Creates a new array, which can lead to performance issues for large datasets. - Can be slower compared to the spread operator. * **Spread operator (`...`):** + Pros: - More concise and expressive than `concat()`. - Creates a shallow copy of the original arrays, preserving references. + Cons: - Requires support for ES6 syntax (introduced in Chrome 57, Firefox 48, etc.). - Can be slower due to the creation of a new array object. **Library and its purpose:** There is no explicit library used in this benchmark. The spread operator is a built-in JavaScript feature introduced in ECMAScript 2015 (ES6). **Special JS feature or syntax:** The benchmark uses the spread operator (`...`), which was introduced in ES6. This allows for more concise and expressive array concatenation. **Other considerations:** * Performance: The benchmark measures the execution speed of both approaches, likely to determine which one is faster. * Memory usage: Creating a new array using `concat()` may lead to higher memory usage compared to the spread operator, which creates a shallow copy of the original arrays. **Alternative approaches:** 1. **Array.prototype.push()**: This method modifies the original array in-place, eliminating the need for a new array or spreading elements. 2. **Array.prototype.splice()**: This method also modifies the original array in-place and can be used to concatenate arrays. 3. **Array.prototype.slice() + Array.prototype.concat()**: Another approach is to use `slice()` to create a shallow copy of the original array and then concatenate it using `concat()`. However, these alternatives are not explicitly tested or compared in this benchmark, focusing instead on the spread operator versus `concat()`.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs push w/ spread operator vs spread operator
array concat vs spread create new array
Array.prototype.concat vs spread operator (new try)
Comments
Confirm delete:
Do you really want to delete benchmark?