Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.at vs index (last element)
(version: 1)
Measures performance between using arr.at(-1) to access the last element in the array and arr[arr.length - 1]
Comparing performance of:
Array.at() vs Index
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const arr = Array(1000000).fill(0).map((_, i) => i)
Tests:
Array.at()
const val = arr.at(-1);
Index
const val = arr[arr.length - 1];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.at()
Index
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.at()
106063928.0 Ops/sec
Index
113171104.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON measures the performance of two different methods for accessing the last element of an array in JavaScript: using the `Array.at()` method and traditional array indexing. ### Options Compared 1. **Array.at(-1)**: - This method was introduced in ECMAScript 2022 (ES13) and allows for accessing elements from the end of an array using negative integers. In this case, `arr.at(-1)` retrieves the last element of the array. - **Pros**: - Semantics: The syntax clearly indicates that you're accessing an element relative to the end of the array, which can improve code readability. - Less prone to errors: There’s no need to calculate the index based on the length of the array, reducing the chance of off-by-one errors. - **Cons**: - Browser support: As a newer feature, older browsers might not support it, though this is becoming less of a concern as time goes on. 2. **arr[arr.length - 1]**: - This is the traditional way to access the last element of an array by calculating its index. - **Pros**: - Familiarity: Most JavaScript developers are familiar with this approach as it has been used for a long time. - Broad support: It works in all environments that support JavaScript, making it a safe choice for backward compatibility. - **Cons**: - Error-prone: It requires manual calculation of the index, which can lead to off-by-one errors, particularly in complex code snippets. - Readability: For some developers, accessing the last element this way may be less intuitive compared to `Array.at()`. ### Performance Results The benchmark results indicate the following: - `Array.at(-1)` achieves approximately **71,040,160 executions per second**. - Array index access (`arr[arr.length - 1]`) achieves approximately **64,184,620 executions per second**. Given these results, `Array.at()` is faster in this context, which could influence developers when considering performance-critical sections of their code. ### Other Considerations - **Code Readability and Maintenance**: While performance is important, code readability and maintainability also play a crucial role in software development. In this case, adopting `Array.at()` could be beneficial for team members unfamiliar with indexing. - **Long-term Support and Future-proofing**: Using modern language features can be advantageous as the web ecosystem evolves, and more developers adopt the latest standards. ### Alternatives Aside from the two approaches tested, alternatives for accessing the last element could include: - Using libraries like **Lodash** or **Underscore.js** which offer utility functions (e.g., `_.last(array)`), though this may introduce additional overhead. - Using TypeScript or other typings can offer safer coding practices to minimize off-by-one errors when using indexes, although it wouldn't fundamentally change how the elements are accessed. In summary, the benchmark provides a clear comparison between two methods of accessing the last element of an array. The performance metrics indicate that `Array.at()` provides a more efficient approach, while also enhancing readability and maintainability. The choice between the two should consider factors, including performance, readability, and browser support.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]`
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
arr.at(-1) vs arr[arr.length - 1]
Test slice vs array.length accessing last element
Array.at(-1) vs. Array.length - 1
`array.slice(-1)[0]` vs `array[array.length - 1]`
Array.Prototype.at vs index
arr.at(-1) vs arr[arr.length-1]
Array.Prototype.at vs index (negative)
Comments
Confirm delete:
Do you really want to delete benchmark?