Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
`array.slice(-1)[0]` vs `array[array.length - 1]`
(version: 0)
Ways to get the last item in an array
Comparing performance of:
`array.slice(-1)[0]` vs `array[array.length-1]`
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from(Array(100).keys());
Tests:
`array.slice(-1)[0]`
arr.slice(-1);
`array[array.length-1]`
arr[arr.length-1];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
`array.slice(-1)[0]`
`array[array.length-1]`
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
`array.slice(-1)[0]`
5970629.5 Ops/sec
`array[array.length-1]`
3631181.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and explain what is being tested. **What is being tested?** The benchmark measures the performance of two ways to get the last item in an array: using `array.slice(-1)[0]` or `array[array.length - 1]`. The goal is to determine which method is faster. **Options compared** There are two options being compared: 1. **Using `array.slice(-1)[0]`**: This method creates a new array with the last element and then extracts it using `[0]`. 2. **Using `array[array.length - 1]`**: This method directly accesses the last element of the original array. **Pros and Cons** * **Using `array.slice(-1)[0]`**: + Pros: Portable across different browsers, easier to understand for developers familiar with JavaScript. + Cons: Creates a new array, which can lead to additional memory allocation and garbage collection overhead. * **Using `array[array.length - 1]`**: + Pros: Direct access to the last element, no additional memory allocation or garbage collection overhead. + Cons: May not work as expected in older browsers that don't support negative indices, and its syntax might be less familiar to some developers. **Other considerations** * **Libraries**: Neither of these methods relies on any external libraries. However, if the benchmark were testing a different scenario, using a library like `lodash` (which provides a `last()` method) could provide an alternative. * **JS features**: The test doesn't rely on any special JavaScript features or syntax beyond basic array manipulation. **Alternatives** If you wanted to create similar benchmarks for other scenarios, you could consider testing: * Different methods for getting the first element of an array (e.g., `array[0]`, `array.slice(0, 1)`) * Methods for sorting arrays (e.g., using the `sort()` method vs. a custom comparison function) * Ways to optimize array performance (e.g., using sparse arrays or optimized data structures) Keep in mind that the choice of benchmark scenario depends on your specific use case and goals.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]`
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
slice vs length-1
Test slice vs array.length accessing last element
Comments
Confirm delete:
Do you really want to delete benchmark?