Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array concat vs spread operator vs push with random array 10000
(version: 2)
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:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function randomArray(length, max) { return Array.apply(null, Array(length)).map(function() { return Math.round(Math.random() * max); }); } var arr = randomArray(10000, 15);
Tests:
Array.prototype.concat
var other = arr.concat(1);
spread operator
var other = [...arr, 1]
Push
var other = arr.push(1);
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 break down the provided benchmark test cases. **Benchmark Definition** The main goal of this benchmark is to compare three methods for adding an element to an array: 1. `Array.prototype.concat()` 2. The new ES6 spread operator (`...`) 3. The `push()` method These methods are used to add a single element (in this case, `1`) to the end of an array created with 10,000 random integers. **Options Compared** The benchmark compares the performance of these three methods: * `Array.prototype.concat()`: A traditional method for concatenating arrays. * The new ES6 spread operator (`...`): A syntax introduced in modern JavaScript versions that allows creating a new array by spreading an existing array or values. * The `push()` method: A simple way to add an element to the end of an array. **Pros and Cons** Here are some general pros and cons for each approach: * `Array.prototype.concat()`: + Pros: Well-established, easy to understand, and widely supported across browsers. + Cons: Can be slower than other methods due to its overhead. * The new ES6 spread operator (`...`): + Pros: Modern syntax, efficient, and widely supported in modern browsers. + Cons: Requires a relatively recent JavaScript version (ES6+) to work. * `push()` method: + Pros: Simple, fast, and widely supported across browsers. + Cons: Not as well-established or readable as the other two methods. **Library/Utility Functions** In this benchmark, there is no specific library or utility function being used. The focus is solely on comparing the performance of three native JavaScript methods. **Special JS Features/Syntax** The benchmark uses the new ES6 spread operator (`...`), which was introduced in modern JavaScript versions (ES6+). This syntax allows creating a new array by spreading an existing array or values. It's a relatively recent feature, and its use is optional. **Alternatives** If you wanted to test these methods on older browsers that don't support the ES6 spread operator, you could modify the benchmark to use a different method, such as: * `Array.prototype.slice()`: A way to create a new array by copying an existing one. * `Array.prototype.splice()`: A way to add or remove elements from an array. Keep in mind that these alternatives would likely change the performance characteristics of the benchmark.
Related benchmarks:
Array.prototype.concat vs spread operator - Immutable version
concat 2 arrays: Array.prototype.concat vs spread operator
Native concat() vs ES6 spread [2]
Array.prototype.concat vs spread operator on large array
Comments
Confirm delete:
Do you really want to delete benchmark?