Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.at(-1) vs. Array.length - 1
(version: 0)
Comparing performance of:
.at(-1) vs length - 1 vs const LEN - 1
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var LEN = 3000; var arr = new Array(LEN).fill(0).map((_, i) => i);
Tests:
.at(-1)
var other = arr.at(-1);
length - 1
var other = arr[arr.length - 1]
const LEN - 1
var other = arr[LEN - 1]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
.at(-1)
length - 1
const LEN - 1
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.at(-1)
110957560.0 Ops/sec
length - 1
121111184.0 Ops/sec
const LEN - 1
115323264.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark code to understand what's being tested. **Benchmark Definition** The benchmark definition is a single line of JavaScript that compares the performance of three different approaches: * `Array.at(-1)` * `arr.length - 1` * `const LEN - 1` In this case, we're using a variable `LEN` initialized with 3000 to create an array with that many elements. Each element is assigned a value of its index `i` using the `map()` method. **Library Usage** There's no explicit library mentioned in the benchmark definition or test cases. However, it's likely that some libraries might be used indirectly through browser-specific features or built-in methods. **JavaScript Features/Syntax** The benchmark uses the following JavaScript feature: * The spread operator (`=>`) is used to define an anonymous function (a lambda expression) within the `map()` method. * The `const` keyword is used to declare a variable `LEN` with a value that will be evaluated at compile-time. **Options Compared** The three options being compared are: 1. **`.at(-1)`**: This is a modern JavaScript feature introduced in ECMAScript 2019, which allows accessing the last element of an array by its index. 2. **`arr.length - 1`**: This approach uses the `length` property to access the length of the array and then subtracts 1 from it to get the index of the last element. 3. **`const LEN - 1`**: This option is a bit more explicit, as it calculates the index by subtracting 1 from the predefined constant `LEN`. **Pros and Cons** Here's a brief summary of each approach: * `.at(-1)`: + Pros: concise and expressive, modern JavaScript feature. + Cons: might not be supported in older browsers or environments. * `arr.length - 1`: + Pros: widely supported, easy to read and understand. + Cons: requires two separate operations (getting the length and subtracting 1). * `const LEN - 1`: + Pros: explicit and controlled, uses a constant value. + Cons: less concise than `.at(-1)` or `arr.length - 1`, might be seen as more verbose. **Other Alternatives** Some alternative approaches that are not being tested in this benchmark include: * Using `arr[arr.length - 2]` to access the second-to-last element (which would require two operations: getting the length and subtracting 1). * Using a traditional loop or recursion to iterate over the array elements. * Using other array methods, such as `find()` or `reduce()`, which might have different performance characteristics. Keep in mind that this benchmark is focused on comparing the performance of these three specific approaches.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
array[array.length - 1] vs array.at(-1)
arr.at(-1) vs arr[arr.length - 1]
at(-1) vs slice(-1)[0] vs length - 1
Comments
Confirm delete:
Do you really want to delete benchmark?