<div id=''></div>
var array = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
array.unshift(123)
array.reverse().push(123)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
unshift | |
reverse push reverse |
Test name | Executions per second |
---|---|
unshift | 20193.7 Ops/sec |
reverse push reverse | 1636.4 Ops/sec |
I'll break down the provided benchmark definition and test cases for you.
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The goal is to compare the performance of different approaches in JavaScript.
Benchmark Definition JSON
The provided benchmark definition JSON represents a single benchmark with two test cases:
Main Benchmark
The main benchmark compares three options for adding an element to the end of an array:
array.unshift(123)
array.push(123)
array.reverse().push(123)
These three approaches are being compared in terms of their execution time.
Individual Test Cases
Each individual test case is a separate benchmark that tests one specific approach:
array.unshift(123)
array.reverse().push(123)
Libraries and Special JavaScript Features
None of the test cases use any specific libraries or special JavaScript features beyond standard ECMAScript syntax.
Options Compared
The three options being compared in the main benchmark are:
unshift()
: Adds an element to the beginning of an array.push()
: Adds an element to the end of an array.reverse().push()
: Reverses the array, pushes an element onto it, and then reverses it again.Other Alternatives
There are other ways to add elements to an array in JavaScript:
concat()
: Concatenates two arrays using the spread operator ([...array].concat(element)
).Array.prototype.set()
or TypedArray.prototype.set()
: Sets a value at a specific index.Buffer
or other low-level data structures.Keep in mind that these alternatives may have different performance characteristics and use cases compared to the options being tested.
Execution Results
The latest benchmark result shows two test runs with execution times:
These results indicate that array.unshift(123)
is significantly faster than array.reverse().push(123)
, likely due to the efficiency of unshift()
and the inefficiency of the reversal operation in reverse.push()
.