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(-1)
Created:
2 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 < 1000; i++) { const v = arr.slice(-1)[0]; }
Length - 1
for (let i = 0; i < 1000; i++) { const v = arr[arr.length - 1]; }
at(-1)
for (let i = 0; i < 1000; 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(-1)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
15 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Slice
35755.0 Ops/sec
Length - 1
1021982.6 Ops/sec
at(-1)
843060.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided benchmark measures three different approaches to access the last element in an array: 1. **Slice**: Using `arr.slice(-1)[0]` to extract the last element from the array using the `slice()` method. 2. **Length - 1**: Using `arr[arr.length - 1]` to access the last element by calculating the length of the array and subtracting 1. 3. **at()**: Using `arr.at(-1)` to access the last element using the `at()` method, which was introduced in ECMAScript 2020. **Options compared** The benchmark compares these three approaches for accessing the last element in an array, specifically when executing a loop 1000 times. The options are: * Slice: uses `arr.slice(-1)[0]` * Length - 1: uses `arr[arr.length - 1]` * at(): uses `arr.at(-1)` **Pros and Cons of each approach** * **Slice**: This approach has the advantage of being concise and easy to read, but it may incur additional overhead due to the creation of a new array slice. On the other hand, it can be slower than accessing elements directly by index. * **Length - 1**: This approach is likely to be faster than Slice because it avoids creating an extra array slice. However, it requires calculating the length of the array and subtracting 1, which may add some overhead. * **at()**: The `at()` method was designed specifically for accessing elements by index, making it potentially faster and more efficient than the other two approaches. However, its use is still relatively rare, and some older browsers might not support it. **Library and purpose** There are no libraries used in this benchmark beyond the standard JavaScript features. **Special JS feature or syntax** The `at()` method was introduced in ECMAScript 2020 as a part of the standard. It's designed to provide a more concise way of accessing elements at specific indices, making it easier to write code that accesses arrays by index. **Other considerations** When optimizing performance-critical code, it's essential to consider the trade-offs between different approaches and the specific requirements of your use case. In general: * Conciseness is often more important than raw speed. * Creating extra copies of data can add overhead, so it's essential to minimize them when possible. **Alternatives** If you're looking for alternative ways to access elements in an array, some other options include: * **for...of**: Using a `for...of` loop to iterate over the array and access each element by index. * **forEach()**: Using the `forEach()` method to execute a callback function on each element of the array. * **Map()**: Using the `Map()` constructor to create a new map where each key-value pair represents an element in the original array. These alternatives may have different performance characteristics depending on your specific use case and requirements.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]`
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
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?