Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Last item in array Slice vs Length - 1 vs .at
(version: 0)
Comparing performance of:
Slice vs Length - 1 vs .at
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
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
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into what this benchmark is testing. **Benchmark Overview** This benchmark compares three different ways to access the last item in an array of 1 million elements: 1. `arr.slice(-1)[0]` (Slice method) 2. `arr[arr.length - 1]` (Index access with length minus one) 3. `arr.at(-1)` (.at method) The goal is to determine which approach is the fastest in terms of execution time. **Test Cases** Each test case runs a loop for 1000 iterations, and within each iteration, it accesses the last item in the array using one of the three methods mentioned above. Here are the details: 1. **Slice**: This test uses `arr.slice(-1)[0]`, which creates a new array containing only the last element (via `slice`) and then accesses that element via index `[0]`. 2. **Length - 1**: This test uses `arr[arr.length - 1]`, which accesses the array's length property and subtracts 1 to get the index of the last item. 3. **.at**: This test uses `arr.at(-1)`, a new method introduced in ECMAScript 2022 (ES2022), which takes an index as an argument and returns the element at that position. **Library** None mentioned in this specific benchmark. **Special JS Features or Syntax** Yes, two special features are used: 1. **Array.prototype.slice()**: This method creates a shallow copy of a portion of an array. 2. **Array.at()` : Introduced in ECMAScript 2022 (ES2022), this method returns the element at the specified index. **Pros/Cons of each approach** Here's a brief summary: 1. **Slice**: * Pros: Simple and easy to understand, works across different browsers. * Cons: Creates an extra array object, which can be memory-intensive for large arrays. 2. **Length - 1**: * Pros: Fast and efficient, as it only involves a simple index calculation. * Cons: Can be slower than other methods when dealing with very large arrays or sparse arrays. 3. **.at**: * Pros: Introduced in ES2022, provides a more elegant way to access array elements directly. * Cons: May not work in older browsers that don't support the `.at()` method. Other alternatives: 1. **Pop()**: This method removes and returns the last element of an array. While not exactly what we're testing here, it's another way to access the last item in an array. 2. **Array.slice().length - 1**: A variation on the `Slice` approach that uses the `slice()` method without `[0]`, which might be slightly faster. Keep in mind that these results are specific to this benchmark and may not reflect real-world performance differences, as they depend on various factors like browser engine optimizations and array sizes.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
Last item in array Slice vs Length - 1
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?