var arr = [];
arr.push(1)
arr.unshift(1)
arr = [1, arr]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
arrayPush-1 | |
arrayUnshift-1 | |
arrayConact-1 |
Test name | Executions per second |
---|---|
arrayPush-1 | 9040752.0 Ops/sec |
arrayUnshift-1 | 11.4 Ops/sec |
arrayConact-1 | 0.3 Ops/sec |
I'd be happy to help you understand the JavaScript microbenchmarking test case on MeasureThat.net.
Benchmark Overview
The benchmark compares three different approaches for adding an element to an array in JavaScript:
arr.push(1)
arr.unshift(1)
arr = [1, ...arr]
Approaches Compared
Here's a brief description of each approach:
arr.push(1)
: This method adds the specified value to the end of the array.arr.unshift(1)
: This method adds the specified value to the beginning of the array.arr = [1, ...arr]
: This is a more modern approach using spread syntax. It creates a new array by concatenating the original array with the element 1
.Pros and Cons
Here are some pros and cons for each approach:
arr.push(1)
:arr.unshift(1)
:arr = [1, ...arr]
:Library Used
None. This benchmark uses only built-in JavaScript features.
Special JS Feature/Syntax
The ...
spread operator used in the third approach (arr = [1, ...arr]
) is a modern syntax introduced in ECMAScript 2015 (ES6). It's supported by most modern browsers and Node.js versions.
Other Alternatives
If you want to test other array operations or different algorithms, MeasureThat.net provides many benchmarks for various JavaScript scenarios. Some examples include:
arr.concat([1])
arr.slice(0, 10)
Please note that the benchmark results may vary depending on the specific browser, device platform, operating system, and other factors.