Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Getting last element of array (index/slice/at/pop)
(version: 1)
Comparing performance of:
slice vs length vs at vs pop
Created:
9 months ago
by:
Registered User
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
for (let i = 0; i < 1000; i++) { const v = arr[arr.length - 1]; }
at
for (let i = 0; i < 1000; i++) { const v = arr.at(-1); }
pop
for (let i = 0; i < 1000; i++) { const v = arr.pop(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
slice
length
at
pop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 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
slice
71731.7 Ops/sec
length
579012.8 Ops/sec
at
580081.6 Ops/sec
pop
884225.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 9 months ago):
The provided benchmark tests the performance of different techniques for retrieving the last element from an array in JavaScript. The benchmark compares four distinct approaches: 1. **`slice` Method**: ```javascript for (let i = 0; i < 1000; i++) { const v = arr.slice(-1)[0]; } ``` - **Pros**: The `slice` method can create a new array from a specified range, making it flexible for various use cases. - **Cons**: This method creates a temporary array, which incurs additional overhead for memory allocation and garbage collection. This makes it generally slower for simply accessing the last element. 2. **Using `length` Property**: ```javascript for (let i = 0; i < 1000; i++) { const v = arr[arr.length - 1]; } ``` - **Pros**: This method directly accesses the last element using the array's length property. It is straightforward and efficient since it does not involve creating any additional arrays. - **Cons**: None significant for the purpose of simply accessing the last element, although it may be less readable to beginners than more descriptive methods. 3. **`at` Method**: ```javascript for (let i = 0; i < 1000; i++) { const v = arr.at(-1); } ``` - **Pros**: This method was introduced in ES2022 and allows for negative indexing, providing a concise way to access elements from the end of an array. It's typically as efficient as directly using the length property. - **Cons**: This method may not be supported in all older environments before ES2022, which could lead to compatibility issues. 4. **`pop` Method**: ```javascript for (let i = 0; i < 1000; i++) { const v = arr.pop(); } ``` - **Pros**: The `pop` method removes and returns the last element of the array. It is useful when you need to modify the original array by removing elements. - **Cons**: For simply accessing the last element, it is inefficient, as it modifies the array and incurs more overhead than required. Furthermore, repeated calls to `pop` can lead to an empty array if called enough times. ### Benchmark Results The results show the number of executions per second for each method: - **`at` Method**: 2,940,804.75 executions/second - **`length` Method**: 2,936,909.25 executions/second - **`pop` Method**: 1,540,396.125 executions/second - **`slice` Method**: 80,311.28125 executions/second From the results, the `at` and `length` methods perform the best, being nearly equivalent in speed, while `pop` is significantly slower due to its modifying nature, and `slice` is the slowest due to the creation of a temporary array. ### Alternatives 1. **Using Loops**: Instead of using built-in methods, one could write a loop to iterate from the end of the array to find the last element. However, this tends to be less efficient and more verbose. 2. **Third-party Libraries**: Libraries like Lodash offer utility functions for manipulating arrays, including accessing elements. However, using additional libraries can add to the size and complexity of the code without significant performance benefits in this case. In conclusion, the best approaches to retrieve the last element of an array in JavaScript are using either the `length` property or the `at` method, both of which provide optimal performance without modifying the array. The `pop` method should generally be avoided for this purpose, and `slice` should only be used when there's a specific need to create a new array.
Related benchmarks:
Last item in array Slice vs Length - 1
Last item in array slice/0 vs length - 1 vs slice/pop vs slice/shift
Last item in array Slice vs Length - 1 vs array.at
Last item in array Slice vs Length - 1 vs At -1
Last item in array Slice vs Length - 1 vs .at
Last item in array Slice vs Length - 1 vs at(-1)
Getting last element of array
Array last index
Getting last element of array (no cash arr.length)
Comments
Confirm delete:
Do you really want to delete benchmark?