var array = [1,2,3];
var d = array[2];
var z = array.at(2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array[2] | |
array.at(2) |
Test name | Executions per second |
---|---|
array[2] | 29390016.0 Ops/sec |
array.at(2) | 29816042.0 Ops/sec |
What is being tested?
MeasureThat.net is testing the performance difference between accessing the third element of an array using two different methods: array[2]
and array.at(2)
. The benchmark is designed to measure which method is faster in terms of executions per second.
Options compared
There are only two options being compared:
array[2]
: This method uses the bracket notation to access the element at index 2 of the array.array.at(2)
: This method uses the at()
method, which is a relatively new addition to the JavaScript language (introduced in ECMAScript 2019), to access the element at index 2 of the array.Pros and Cons
array[2]
Pros:
Cons:
array.at(2)
Pros:
array[2]
, especially for large arrays (because it avoids creating intermediate elements)Cons:
Library usage
Neither of these methods uses any external libraries. They are built-in JavaScript methods.
Special JS feature or syntax
array.at(2)
uses a relatively new feature in JavaScript called "template literals" (specifically, the at()
method). This feature is not widely supported and may require explicit configuration to work.
Other considerations
[1, 2, 3]
) implies that the benchmark is trying to simulate a scenario where performance is critical.Other alternatives
If you're looking for alternative methods to access elements at specific indices in arrays, some other options include:
array[2]
(the same method being compared)array[index]
(a more general syntax that works for any index)Math.abs(array.indexOf(element) - 2)
(a method that finds the index of the element and subtracts 2 to get the desired value)However, these alternatives may not be as efficient or expressive as the at()
method in modern browsers.