Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get last element of array2
(version: 0)
Comparing performance of:
length vs at vs slice
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(100000000).fill(0);
Tests:
length
var a = arr[arr.length - 1];
at
var b = arr.at(-1);
slice
var c = arr.slice(-1)[0]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
length
at
slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
length
5251306.5 Ops/sec
at
9310570.0 Ops/sec
slice
6900354.5 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares three different ways to access the last element of a very large array in JavaScript. Let's break down each approach: **1. `length`:** This method uses the `length` property of the array to get the index of the last element and then accesses that element directly. - **Code:** `var a = arr[arr.length - 1];` * **Pros:** This is often considered the most straightforward and intuitive way to access the last element. It's simple to understand and implement. * **Cons:** In some situations, it can be slightly less performant than other methods, especially with very large arrays. **2. `at()`:** The `at()` method allows you to access elements in an array by index, even negative indices. `-1` refers to the last element. - **Code:** `var b = arr.at(-1);` * **Pros:** Introduced in ECMAScript 2019, this method is often faster than using `length` and provides a more explicit way to access elements by index. * **Cons:** Not all JavaScript environments support it as it's relatively new. If you need wide browser compatibility, consider alternative methods. **3. `slice()`:** The `slice()` method creates a shallow copy of a portion of an array. Using `slice(-1)[0]`, we take a slice starting from the last element and retrieve the first (and only) element in that slice. - **Code:** `var c = arr.slice(-1)[0]` * **Pros:** Can be useful for situations where you need to work with a subarray containing the last element, not just access it directly. It's also more explicit about creating a copy of the data. * **Cons:** Slightly more verbose than `length` or `at()`, and can potentially have a minor performance overhead due to creating a new array. **Other Considerations:** * **Array Size:** For very large arrays, the difference in performance between these methods might become more significant. * **Task:** If you only need the last element, `at()` is generally recommended for its simplicity and performance. Let me know if you'd like to explore any of these aspects further!
Related benchmarks:
Array clone
array last element big data
shallow copy of 6M elements array
Clone Array - 08/02/2024
Comments
Confirm delete:
Do you really want to delete benchmark?