Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
last item in array [length-1] vs slice(-1) vs pop
(version: 0)
Comparing performance of:
index[length - 1] vs Array.slice(-1)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const elements = document.all; window.arr = Array.from(elements);
Tests:
index[length - 1]
const arr = window.arr; const last = arr[arr.length-1];
Array.slice(-1)
const arr = window.arr; const last = arr.slice(-1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
index[length - 1]
Array.slice(-1)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
index[length - 1]
155631248.0 Ops/sec
Array.slice(-1)
56764428.0 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 two ways to access the last element in an array: 1. Direct indexing (`arr[arr.length-1]`) 2. Using `Array.prototype.slice(-1)` method **Test Case 1: Direct Indexing (`index[length - 1]`)** In this test case, a constant reference to an array (`const arr = window.arr`) is created and stored in the scope. Then, a variable `last` is assigned the value of the last element of the array using direct indexing (`arr[arr.length-1]`). The test measures how fast this approach can be executed. **Test Case 2: Array.prototype.slice(-1)** In this test case, a constant reference to an array (`const arr = window.arr`) is created and stored in the scope. Then, a variable `last` is assigned the value of the last element of the array using the `Array.prototype.slice(-1)` method. This approach creates a new array with only the last element and returns it. **Comparison** Both approaches have their pros and cons: * **Direct Indexing (`arr[arr.length-1]`)** + Pros: - Accesses the last element directly, which is likely to be faster. - Uses less memory since no new array is created. + Cons: - May not work as expected if the array is modified or extended after indexing. - Can lead to stack overflow if the array index exceeds the maximum allowed value. * **Array.prototype.slice(-1)** method + Pros: - Creates a new array with only the last element, which can be useful for other operations. - More flexible and safe than direct indexing since it doesn't rely on array indices. + Cons: - Creates a new array, which may increase memory usage. - May be slower than direct indexing due to the overhead of creating and returning a new array. **Library Usage** The test case uses the `Array.from()` method, which is part of the ECMAScript standard. However, it's not explicitly used in this benchmark. Instead, it's assumed that the `window.arr` variable is an array created using this method during script preparation. **JavaScript Features/Syntax** This benchmark does not use any special JavaScript features or syntax beyond the standard ECMAScript. **Other Alternatives** If you want to access the last element of an array, other approaches include: * Using the `at()` method (introduced in ECMAScript 2020) which returns a new iterator object that iterates over the elements in sequence: ```javascript arr.at(-1); ``` * Using a simple loop to iterate through the array and extract the last element: ```javascript for (let i = arr.length - 1; i >= 0; i--) { const lastElement = arr[i]; // ... } ``` Keep in mind that these alternatives may have different performance characteristics compared to direct indexing or the `Array.prototype.slice(-1)` method.
Related benchmarks:
Last with slice, pop vs index
Slice vs Pop vs last item
Array Slice vs Pop
Slice vs Pop return value
Last element slice vs pop
Comments
Confirm delete:
Do you really want to delete benchmark?