Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator vs contact empty array with 2 arrays
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.concat vs spread operator vs Concat empty array
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].concat(params);
spread operator
var params = [ "hello", true, 7 ] var other = [ 1, 2, ...params ]
Concat empty array
var params = [ "hello", true, 7 ]; var other = [].concat([1, 2], params);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
Concat empty array
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 compares three different ways to combine two arrays in JavaScript: **1. `Array.prototype.concat`:** This is the traditional method using the built-in `concat()` function of the array object. * **Code Example:** `[1, 2].concat([ "hello", true, 7 ])` * **Pros:** Widely known and understood by developers. It works reliably across different JavaScript environments. * **Cons:** Can be slower than the spread operator, especially for larger arrays, due to potential array copying. **2. Spread Operator (`...`)**: Introduced in ES6, this syntax expands the elements of an array directly into another array. * **Code Example:** `[1, 2, ...["hello", true, 7]]` * **Pros:** Generally faster than `concat()` for larger arrays because it avoids unnecessary copying. More concise and readable syntax. * **Cons:** Requires modern JavaScript (ES6+) support. **3. Concatenating with an Empty Array:** This involves creating a new array with the elements from the two existing arrays using `concat()`. * **Code Example:** `[].concat([1, 2], ["hello", true, 7])` * **Pros:** Can be used in older JavaScript environments where the spread operator isn't available. * **Cons:** Less concise and potentially less performant than other options. **Other Alternatives:** While not directly tested here, there are other ways to combine arrays: * **`Array.of()`**: Creates a new array from individual arguments, which can be combined with `concat()`. * **Functional Programming Techniques**: Libraries like Ramda offer functional functions for array manipulation and combination. **Remember:** Benchmark results can vary depending on factors like browser, hardware, and JavaScript engine implementation. The best approach often depends on the specific context and performance requirements of your application.
Related benchmarks:
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (add)
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?