var array = [1,2,3];
var len = array.len;
var n = 1000;
let t = 0;
for(let i = 0; i < n; i++) {
t+= array[len - 1];
}
let t = 0;
for(let i = 0; i < n; i++) {
t+= array.at(-1);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array[len] | |
array.at(-1) |
Test name | Executions per second |
---|---|
array[len] | 9007.0 Ops/sec |
array.at(-1) | 14612.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The provided JSON represents a benchmark definition, which is a set of instructions that defines how to test a specific piece of code. In this case, we have two benchmark definitions:
"Another array[len - 1] vs array.at(-1) "
: This benchmark tests the performance difference between accessing an element at the end of an array using the len
property and the at()
method.Script Preparation Code
The script preparation code is used to initialize variables that are needed for the benchmark test:
var array = [1, 2, 3];
var len = array.len;
var n = 1000;
However, there's a problem. In JavaScript, arrays do not have a len
property. Instead, you can use the length
property to get the length of an array.
Let's assume that the script preparation code should be:
var array = [1, 2, 3];
var n = 1000;
And we'll update the script preparation code in our explanation accordingly.
Benchmark Test Cases
We have two benchmark test cases:
"array[len]"
:
This test case uses the len
property to access an element at the end of the array."array.at(-1)"
:
This test case uses the at()
method to access an element at the end of the array.Library Used
In this benchmark, the at()
method is used, which is a part of the ECMAScript 2019 standard (ES2019). The at()
method allows you to access elements in an array using a numeric index.
The purpose of the at()
method is to provide a more concise and readable way to access elements in an array. Instead of using array[array.length - 1]
, you can use array.at(-1)
.
Pros and Cons
Using the at()
method has several pros:
length
property or calculate the index manually.However, there are some cons to using the at()
method:
at()
method returns an undefined value if the array is empty. You should check for this before accessing elements.Other Alternatives
If you need to access elements at the end of an array in a way that's not using the len
property or the at()
method, you can use other methods like:
length
property and calculating the index manually: array[array.length - 1]
reverse()
method to reverse the array and access elements from the end: array.reverse() && array[0]
However, these alternatives are generally less concise and readable than using the at()
method.
Conclusion
In conclusion, the benchmark tests the performance difference between accessing an element at the end of an array using the len
property (which is not a valid way to access elements in JavaScript) versus the at()
method. The results show that the at()
method is faster than using the len
property.