var arr = Array(86400).fill('');
arr = ['', arr]
arr.unshift('')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread | |
unshift |
Test name | Executions per second |
---|---|
spread | 1397.0 Ops/sec |
unshift | 34929.0 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this specific benchmark.
Benchmark Overview
The benchmark is designed to compare two approaches: using the spread operator (...
) to add elements to an array, and using the unshift()
method to add elements to the beginning of an array. The benchmark aims to determine which approach is faster and more efficient.
Options Compared
There are only two options being compared:
...
): This operator creates a new array by spreading the existing array's elements. In this case, it adds 86400 empty strings to the end of the original array.Pros and Cons
...
):Library and Syntax
There is no specific library used in this benchmark. However, JavaScript's built-in Array
methods are being utilized.
Special JS Feature or Syntax (Not applicable)
No special features or syntax are being tested or utilized in this benchmark.
Alternative Approaches
Other approaches that could be used to compare the performance of adding elements to an array include:
concat()
method instead of the spread operator.arr.push('')
).Benchmark Preparation Code
The preparation code creates an empty array with 86400 elements and assigns it to the variable arr
. This ensures that both tests are starting from the same initial state.
Individual Test Cases
Each test case has a unique benchmark definition and test name:
arr = ['', ...arr]
) and is named "spread".unshift()
method (arr.unshift('')
) and is named "unshift".Latest Benchmark Result
The latest results show that Chrome 107 on a Desktop device with Windows operating system has performed the following execution rates:
This suggests that, in this specific scenario, using unshift()
is significantly faster than using the spread operator for adding elements to an array.