Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread operator vs push (v2)
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method and push
Comparing performance of:
Array.prototype.concat vs spread operator vs Push
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ] params.forEach((el) => other.concat(el))
spread operator
var params = [ "hello", true, 7 ] var other = [ 1, 2 ] params.forEach((el) => other = [...other, el])
Push
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ]; params.forEach((el) => other.push(el))
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
Push
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 dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The benchmark compares three approaches for adding elements to an array: using the `concat()` method, the spread operator (`...`), and the `push()` method. **Approaches Compared** 1. **Array.prototype.concat()**: This is a traditional way of concatenating arrays in JavaScript. 2. **Spread Operator ( ... )**: The spread operator was introduced in ES6 as a concise way to create a new array by spreading the elements of an existing array. 3. **push()**: This method adds one or more elements to the end of an array. **Pros and Cons** 1. **Array.prototype.concat()**: * Pros: widely supported, easy to use. * Cons: creates a new array every time it's called, which can lead to memory issues for large datasets. 2. **Spread Operator ( ... )**: * Pros: concise, efficient, and modern syntax. * Cons: not all browsers support it yet, may be slower than other methods in older browsers. 3. **push()**: * Pros: simple, fast, and widely supported. * Cons: can lead to performance issues if used excessively. **Library Used** There is no explicit library mentioned in the benchmark definition or test cases. However, it's likely that the `forEach()` method is used, which is a built-in function in JavaScript. **Special JS Features/Syntax** The spread operator (`...`) was introduced in ES6 and is a relatively new feature in JavaScript. It's not supported in older browsers like Internet Explorer, so it's essential to consider browser compatibility when using this approach. **Other Alternatives** If the spread operator isn't an option due to browser limitations, other alternatives could be: 1. Using `array.concat()` or `Array.prototype.concat()` 2. Using `array.push()` repeatedly (though this can lead to performance issues) Keep in mind that these alternatives may have different performance characteristics and are not as concise as the spread operator. **Benchmark Results** The benchmark results show the execution frequency per second for each approach: * **Push**: 21959044.0 executions/second * **Array.prototype.concat()**: 5085354.0 executions/second * **Spread Operator ( ... )**: 4778425.5 executions/second These results indicate that `push()` is the fastest approach, followed by the spread operator and then `concat()`. However, it's essential to consider other factors like memory usage and browser compatibility when choosing an approach for a specific use case.
Related benchmarks:
Array.prototype.concat vs spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
ES6 Array concat vs spread operator
Array concat vs spread operator vs push with more data
Array.prototype.concat vs spread operator real
Comments
Confirm delete:
Do you really want to delete benchmark?