Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array[array.length - 1] vs array.at(-1) vs array[array.length + -1]
(version: 0)
Comparing performance of:
array[array.length - 1] vs array.at(-1) vs array[array.length + -1]
Created:
4 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[array.length - 1];
array.at(-1)
var z = array.at(-1);
array[array.length + -1]
var e = array[array.length + -1]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array[array.length - 1]
array.at(-1)
array[array.length + -1]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array[array.length - 1]
157769184.0 Ops/sec
array.at(-1)
160245456.0 Ops/sec
array[array.length + -1]
170174160.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided JSON represents a JavaScript microbenchmarking test case on the MeasureThat.net website. The test focuses on comparing three different ways to access the last element of an array: 1. `array[array.length - 1]` 2. `array.at(-1)` 3. `array[array.length + -1]` These approaches are used to measure their performance and determine which one is the most efficient. **Options comparison** The three options being compared are: * `array[array.length - 1]`: This method uses the `-` operator to subtract 1 from the array length, then uses array indexing (`[]`) to access the element at that index. * `array.at(-1)`: This method uses the `at()` method introduced in ECMAScript 2019 (ES10), which allows accessing an element at a specific index without bounds checking. The `-1` indicates the last element of the array. * `array[array.length + -1]`: This method is similar to the first option, but with a twist: it adds `-1` to the array length instead of subtracting 1. **Pros and Cons** Here are some pros and cons of each approach: * `array[array.length - 1]`: + Pros: Wide compatibility (works in older browsers that don't support `at()`), easy to understand. + Cons: Requires bounds checking, can be slower than other methods. * `array.at(-1)`: + Pros: Faster performance, more efficient memory usage, and better bounds checking compared to the first option. + Cons: Requires modern browsers (ECMAScript 2019 or later) that support the `at()` method. * `array[array.length + -1]`: + Pros: Similar performance to `array[at(-1)]`, but with the added benefit of being more readable due to the simpler indexing pattern. + Cons: Requires modern browsers that support JavaScript operators in array indices. **Library and purpose** None of the options rely on a specific library. However, note that some JavaScript engines may provide additional features or optimizations for arrays. **Special JS feature or syntax** The `at()` method is a new feature introduced in ECMAScript 2019 (ES10). It allows accessing an element at a specific index without bounds checking, making it more efficient and convenient than traditional array indexing methods. The `-1` used with `at()` refers to the last element of the array. **Other alternatives** If you're interested in exploring alternative ways to access the last element of an array, here are some options: * `array[-1]`: This method uses a negative index to access the first element from the end of the array. It's not as efficient as `at(-1)` but can be useful in certain scenarios. * `slice().reverse()[0]`: This method creates a reversed copy of the array using `slice()` and then accesses the last element (`[0]`). While it works, it's less efficient than the other options due to the creation of an intermediate array. Keep in mind that these alternatives may have different performance characteristics or requirements (e.g., browser support).
Related benchmarks:
array[array.length - 1] vs array.at(-1)
array[array.length - 1] vs array.at(-1) 3
array[1] vs array.at(1)
array[index] vs array.at(index)
Comments
Confirm delete:
Do you really want to delete benchmark?