function randomArray(length, max) {
return Array.apply(null, Array(length)).map(function() {
return Math.round(Math.random() * max);
});
}
var arr = randomArray(10000, 15);
var other = arr.concat(1);
var other = [arr, 1]
var other = arr.push(1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
Push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 15783.0 Ops/sec |
spread operator | 41593.8 Ops/sec |
Push | 11164103.0 Ops/sec |
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:
Array.prototype.concat()
...
)push()
methodThese 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....
): A syntax introduced in modern JavaScript versions that allows creating a new array by spreading an existing array or values.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()
:...
):push()
method: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.