Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Last item in array Slice vs Length - 1 vs At -1
(version: 0)
Comparing performance of:
Slice vs Length - 1 vs At ES2022 method -1 index
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (let i = 0; i < 1000000; i++) { arr.push(i); }
Tests:
Slice
for (let i = 0; i < 10000; i++) { const v = arr.slice(-1)[0]; }
Length - 1
for (let i = 0; i < 10000; i++) { const v = arr[arr.length - 1]; }
At ES2022 method -1 index
for (let i = 0; i < 10000; i++) { const v = arr.at(-1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Slice
Length - 1
At ES2022 method -1 index
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Slice
1513.8 Ops/sec
Length - 1
1055.8 Ops/sec
At ES2022 method -1 index
2120.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark measures the performance of three different approaches to access the last element of an array: 1. Using `slice()` method with a negative index (`arr.slice(-1)[0]`) 2. Using the `-1` index directly on the array length (`arr[arr.length - 1]`) 3. Using the new ES2022 `at()` method with a negative index (`arr.at(-1)`) **Options Compared** The three options are compared to determine which one is the fastest. **Pros and Cons of Each Approach:** 1. **`slice()` method with a negative index**: This approach creates a new array slice containing only the last element. While it's a simple and intuitive way to access the last element, it may incur additional overhead due to the creation of a new array. 2. **Direct Access using `-1` index**: This approach directly accesses the last element of the array by using the `-1` index. It's a straightforward way to access an element at a specific position in the array. 3. **ES2022 `at()` method with a negative index**: This approach uses the new `at()` method, introduced in ES2022, which provides a safer and more efficient way to access elements at a specific position in the array. **Pros of each approach:** * Direct Access using `-1` index: Simple and fast * `slice()` method with a negative index: Easy to read and maintain **Cons of each approach:** * Direct Access using `-1` index: May not work if the array is empty or has a length less than 0 * `slice()` method with a negative index: Creates a new array, which may incur additional overhead **Library Used:** None (arrays are built-in JavaScript objects) **Special JS Feature/Syntax:** ES2022's `at()` method, introduced in 2019, is used to access elements at a specific position in the array. The `at()` method provides several benefits over traditional indexing: * It throws an error if the index is out of range, making it safer for code that doesn't expect the index to be valid. * It's more efficient than creating a new array or using the `-1` index directly on the array length. **Other Alternatives:** If you don't want to use `slice()` with a negative index or the ES2022 `at()` method, you can also access the last element of an array by: * Using `Math.max(...arr)`, which returns the maximum value in the array * Creating a new variable to hold the length of the array and then accessing the element at that position However, these alternatives may incur additional overhead or have other performance implications. In summary, the benchmark measures the performance of three different approaches to access the last element of an array: `slice()` with a negative index, direct access using `-1` index, and ES2022's `at()` method. The results show that the `at()` method is the fastest approach, followed by direct access using `-1` index, and then `slice()` with a negative index.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
array.prototype.at() vs array[array.length - 1]
Test slice vs array.length accessing last element
`array.slice(-1)[0]` vs `array[array.length - 1]`
at(-1) vs slice(-1)[0] vs length - 1
Comments
Confirm delete:
Do you really want to delete benchmark?