var array = []
for(var i = 0; i < 1000; i++) {
array = array.concat(i)
}
var array = []
for(var i = 0; i < 1000; i++) {
array.push(i)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
concat | |
push |
Test name | Executions per second |
---|---|
concat | 310.1 Ops/sec |
push | 293838.7 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and discussed.
Benchmark Purpose:
The main objective of this benchmark is to compare the performance of two different ways to append elements to an array in JavaScript: concat
and push
.
Script Preparation Code: There is no script preparation code provided for this benchmark. This means that the test cases are simple and don't require any setup or initialization.
Html Preparation Code: There is also no html preparation code provided, which suggests that the benchmark only measures JavaScript performance and doesn't involve any HTML-related overhead.
Test Cases: The two test cases are:
concat
: This test case uses the concat
method to append numbers from 0 to 999 to an empty array.push
: This test case uses the push
method to append numbers from 0 to 999 to an empty array.Library and Purpose:
There is no explicit library mentioned in this benchmark, but it's likely that the benchmark relies on the built-in JavaScript Array
prototype methods (concat
and push
) or a specific testing framework that provides these methods.
Special JS Features/Syntax: There are no special JS features or syntax explicitly used in this benchmark. However, note that some modern browsers (like Chrome) use a technique called "just-in-time" compilation, which can affect performance measurements. MeasureThat.net likely accounts for this by running the benchmarks multiple times and averaging the results.
Comparison of Approaches:
The comparison between concat
and push
methods is based on their performance characteristics:
push
method is generally faster than concatenating arrays. This is because pushing doesn't create a new array object; instead, it modifies the existing one by appending the element to its end.concat
method creates a new array object that contains all elements from both arrays. This can lead to slower performance compared to pushing, especially for large datasets.Pros and Cons:
Pros of using push
:
Cons of using push
:
array.push(i), array.push(j)
)Pros of using concat
:
Cons of using concat
:
Other Alternatives:
If you need to append elements to an array in JavaScript, some alternative approaches include:
for...of
loop and pushing elements onto the array: array = []; for (const value of arr) { array.push(value); }
compact
function: _([].concat(arr)).flatten()
[...]
) followed by push: [...[], ...arr]
However, these alternatives might not always be faster or more efficient than the original push
and concat
methods.
Overall, this benchmark provides a simple and informative comparison of two common JavaScript array modification techniques. It highlights the importance of considering performance characteristics when choosing an approach for specific use cases.