Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread operator (2 separate arrays of same type)
(version: 0)
Compare the ES6 spread operator with the traditional concat() method
Comparing performance of:
Array.prototype.concat vs spread operator
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
const params = [ {id:1}, {id:2}, {id:3} ]; const initial = [ {id:4}, {id:5}, {id:6} ]; const other = initial.concat(params);
spread operator
const params = [ {id:1}, {id:2}, {id:3} ]; const initial = [ {id:4}, {id:5}, {id:6} ]; const other = [ ...initial, ...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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.concat
8935132.0 Ops/sec
spread operator
6256677.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Definition** The benchmark is designed to compare two approaches for concatenating arrays in JavaScript: the traditional `concat()` method and the ES6 spread operator (`...`). **Options Compared** The benchmark compares two options: 1. **Traditional `concat()` method**: This approach uses the `concat()` function to concatenate two arrays. ```javascript const other = initial.concat(params); ``` 2. **ES6 spread operator**: This approach uses the spread operator (`...`) to concatenate two arrays. ```javascript const other = [ ...initial, ...params ]; ``` **Pros and Cons** **Traditional `concat()` method:** Pros: * Well-established and widely supported in older browsers. * Easy to understand and implement. Cons: * Can be slow for large arrays due to the overhead of creating new array objects. * Can lead to unnecessary memory allocation. **ES6 spread operator:** Pros: * Fast and efficient, as it avoids creating new array objects. * Modern and widely supported in recent browsers. Cons: * May require explicit knowledge of the spread operator syntax. * Not well-established in older browsers. **Library and Purpose** In the provided benchmark, there is no specific library being used. The code examples use built-in JavaScript features (i.e., `concat()` and the spread operator). **Special JS Features or Syntax** The benchmark uses ES6 features, specifically: 1. **Spread operator (`...`)**: Introduced in ECMAScript 2015 as a new way to concatenate arrays. 2. **Template literals**: Used for string interpolation in the script preparation code. Other considerations: * The benchmark focuses on measuring the performance difference between two specific approaches, rather than exploring other optimizations or edge cases. * The use of `null` values for `Script Preparation Code` and `Html Preparation Code` suggests that the benchmark might be using a minimal setup to test the array concatenation performance in isolation. **Other Alternatives** If you were to explore alternative approaches to concatenate arrays, some options could include: 1. **Array.prototype.push()**: Using `push()` method to add elements to an existing array. ```javascript const other = initial; other.push(...params); ``` 2. **Set**: Using a Set data structure to avoid duplicates and improve performance for large datasets. ```javascript const other = new Set(params).size > 0 ? params : []; ``` However, these alternatives might have different performance characteristics, trade-offs in terms of memory usage or complexity, and may not be as straightforward to implement as the traditional `concat()` method or ES6 spread operator.
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 real
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?