Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array last item speed
(version: 1)
Comparing performance of:
array[array.length - 1] vs array.slice(-1)[0] vs [...array].reverse()
Created:
10 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ var array = [0,1,2,3,4]
Tests:
array[array.length - 1]
/*When writing async/deferred tests, use `deferred.resolve()` to mark test as done*/ array[array.length - 1]
array.slice(-1)[0]
array.slice(-1)[0]
[...array].reverse()
[...array].reverse()
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.slice(-1)[0]
[...array].reverse()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array[array.length - 1]
140917408.0 Ops/sec
array.slice(-1)[0]
39939796.0 Ops/sec
[...array].reverse()
32908908.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
This benchmark evaluates different methods for retrieving the last item of an array in JavaScript. The objective is to measure and compare the performance of various approaches, specifically how many operations can be executed per second (ExecutionsPerSecond) for each method. ### Test Methods Compared 1. **Direct Indexing: `array[array.length - 1]`** - **Description**: This method retrieves the last element by accessing the array with the length property minus one. It is a straightforward and efficient way to get the last item in an array. - **Performance Result**: 140,917,408 Executions Per Second - **Pros**: - Very efficient with a time complexity of O(1) since it accesses an element by index. - Readable and clear in intent, making it easy to understand for other developers. - **Cons**: - None significant, as it is the optimal solution for this use case. 2. **Using `slice()`: `array.slice(-1)[0]`** - **Description**: This method uses the `slice` function to create a new array that contains only the last element by passing a negative index of `-1`, and then accesses the first (and only) element of that new array. - **Performance Result**: 39,939,796 Executions Per Second - **Pros**: - Can be useful in contexts where you want a copy of the last element in a new array rather than directly accessing it (e.g., avoiding side effects on the original array). - **Cons**: - Less efficient due to the creation of a new array, which adds overhead. Its time complexity is O(n) in terms of memory usage when creating slices. - More complex than the direct indexing method. 3. **Using Spread and `reverse()`: `[...array].reverse()`** - **Description**: This approach first creates a shallow copy of the original array using the spread operator and then reverses it to bring the last item to the first index. - **Performance Result**: 32,908,908 Executions Per Second - **Pros**: - Provides a new version of the array with the elements in reverse order without altering the original. - Useful in scenarios where the entire reversed array might be needed, or immutability is a concern. - **Cons**: - The most inefficient approach in this benchmark, with both time and space complexities being higher due to copying the array and performing the reverse operation. - Unnecessarily complex if the only goal is to retrieve the last element. ### Conclusion - **Best Option**: The first approach (`array[array.length - 1]`) is the most efficient, straightforward, and performant method for accessing the last element of an array. - **Other Considerations**: - Developers should prefer the simplest and most performant options when performance is critical, especially in scenarios involving large arrays or frequent access. - Alternatives, such as using libraries (like Lodash’s `_.last(array)`), could provide more abstracted ways to handle arrays, but they may add additional overhead due to library imports and function complexity, which could detract from performance in scenarios focused purely on speed. ### Alternative Approaches In addition to the methods tested, other possible approaches could include using a utility library (like Lodash) to retrieve the last element, such as `_.last(array)`, or employing iterators and loops for larger arrays, but these generally come with similar or greater performance drawbacks compared to the direct indexing method outlined here. For optimal performance in speed-critical applications, the simplest solution with the lowest overhead is typically the best choice.
Related benchmarks:
reate array by lenght
Test array ops
how to get last array element
Array.from length vs Array
(Fixed) Array.from length vs Array
(Small Array.from length vs Array
Array allocate
Array.from length vs for loop
array speed 2
Comments
Confirm delete:
Do you really want to delete benchmark?