Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Last item in array Slice vs Length - 1 vs array.at
(version: 0)
Comparing performance of:
Slice vs Length - 1 vs Array.at
Created:
3 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]; }
Array.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
Array.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.2:3b
, generated one year ago):
**What is being tested?** The provided JSON represents a JavaScript microbenchmark test case. It's testing three different approaches to access the last item in an array: 1. `arr.slice(-1)[0]`: This approach uses the `slice` method to get the last element of the array and then extracts its value using indexing (`[0]`). 2. `arr[arr.length - 1]`: This approach directly accesses the last element of the array by subtracting 1 from the length of the array. 3. `arr.at(-1)`: This approach uses the new `at` method (introduced in JavaScript ES6) to access the last element of the array. **Options comparison** Here's a brief overview of each approach: * **Slice**: Using `slice` and indexing is a safe way to access array elements, as it returns a new array with the desired elements. However, this approach may incur additional overhead due to the creation of a new array. + Pros: Safe, easy to read. + Cons: May be slower due to array creation. * **Length - 1**: Directly accessing the last element using indexing is a simple and straightforward way to achieve this. It's also a safe approach since it doesn't rely on the `slice` method or any potential errors in indexing. + Pros: Fast, efficient. + Cons: May not be immediately clear to all readers, especially those without familiarity with array indices. * **Array.at**: The new `at` method provides a convenient and expressive way to access specific elements in an array. However, it's only available in modern browsers that support ES6+ features. + Pros: Concise, readable, fast (in supported browsers). + Cons: Requires support for ES6+, which may not be universally compatible. **Library usage** None of the provided benchmark tests use external libraries or frameworks. The code is self-contained and relies on built-in JavaScript functionality. **Special JS features or syntax** The `at` method is a special feature introduced in JavaScript ES6, which allows accessing array elements by their index using dot notation (`arr.at(0)`). This approach is only supported in modern browsers that implement ES6+ features.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]`
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
array.prototype.at() vs array[array.length - 1]
Test slice vs array.length accessing last element
`array.slice(-1)[0]` vs `array[array.length - 1]`
Comments
Confirm delete:
Do you really want to delete benchmark?