Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array[1] vs array.at(1)
(version: 0)
Comparing performance of:
array[array.length - 1] vs array.at(-1)
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [1,2,3];
Tests:
array[array.length - 1]
var d = array[1];
array.at(-1)
var z = array.at(-1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array[array.length - 1]
array.at(-1)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array[array.length - 1]
136308560.0 Ops/sec
array.at(-1)
131069320.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark being tested. **What is being tested?** The provided JSON represents a JavaScript microbenchmark that compares two approaches to access the last element of an array: 1. `array[array.length - 1]` 2. `array.at(-1)` These two expressions are being compared to measure their performance difference. **Options compared:** * `array[array.length - 1]`: This approach uses a property access method to access the last element of the array. It requires calculating the length of the array and then using that value as an index. * `array.at(-1)`: This approach uses the `at()` method, which is a relatively new feature introduced in ECMAScript 2020 (ES2020). The `at()` method allows indexing into arrays with a negative offset, making it more readable and concise than the traditional array indexing syntax. **Pros and cons of each approach:** * `array[array.length - 1]`: + Pros: widely supported in older browsers, easier to understand for those familiar with traditional array indexing. + Cons: can be slower due to the additional calculation required to determine the index. * `array.at(-1)`: + Pros: more readable and concise, faster execution, better support in newer browsers. + Cons: requires a modern browser that supports ES2020, may be less familiar for those without experience with this syntax. **Library usage:** The benchmark does not explicitly use any external libraries. However, it relies on the `at()` method, which is a built-in feature of JavaScript arrays in newer browsers (specifically, Chrome 115 and Safari 537.36). **Special JS features or syntax:** The benchmark uses the new `at()` method, which was introduced in ES2020. This syntax allows for more readable and concise indexing into arrays. **Other alternatives:** If you needed to support older browsers or want a different approach, here are some alternative methods: * Using `slice()` method to extract the last element: `array.slice(-1)[0]` * Using the `[Symbol.iterator]` method to iterate over the array and access the last element: `for (let i of array) { if (i === 2) { console.log(i); } }` Keep in mind that these alternatives may have different performance characteristics compared to the original approaches being tested. **Why this benchmark is interesting:** This benchmark highlights the importance of considering performance and code readability when writing JavaScript. The use of `at()` method demonstrates a new feature that can improve code quality, while still being compatible with older browsers.
Related benchmarks:
array[0] vs array.at(0)
array[1] vs array.at(1) 2
array[] vs array.at()
array[2] vs array.at(2)
array[index] vs array.at(index)
Comments
Confirm delete:
Do you really want to delete benchmark?