Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.prototype.at() vs array[array.length - 1]
(version: 0)
which one is faster for reaching last item of an array
Comparing performance of:
array.prototype.at() vs array[array.length - 1]
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [0, 1, 2, 3];
Tests:
array.prototype.at()
let lastWithAtArray = array.at(-1)
array[array.length - 1]
let lastWithLength = array[array.length - 1]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.prototype.at()
array[array.length - 1]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.prototype.at()
782290560.0 Ops/sec
array[array.length - 1]
1111641088.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark is comparing two approaches to reach the last item of an array: 1. `array.prototype.at()` 2. `array[array.length - 1]` **What is being tested?** The benchmark is testing which approach is faster when trying to access the last element of an array using these methods. **Options compared** There are two options being compared: * Option A: Using `array.prototype.at(-1)` (the "at" method) * Option B: Direct indexing, `array[array.length - 1]` **Pros and Cons** **Option A (`array.prototype.at()`) Pros:** * Provides a more concise and expressive way to access array elements * Can be useful for iterating over arrays in a more functional programming style Cons: * May be slower than direct indexing due to the overhead of calling a method on the prototype chain * Requires modern JavaScript engines that support the `at` method (not all browsers or versions) **Option A (`array.prototype.at()`) Cons:** * Not supported in older browsers or versions * May require additional memory allocation for the method call **Option B (`array[array.length - 1]`) Pros:** * Generally faster than the "at" method due to reduced overhead * Supported by all browsers and versions Cons: * Requires direct indexing, which can be less readable and more error-prone in some cases **Library used** There is no explicit library mentioned in the benchmark definition. However, `array.prototype.at()` is a built-in method introduced in ECMAScript 2019. **Special JS feature/syntax** The "at" method uses a special syntax called "rest operator" (`...`) which was introduced in ECMAScript 2015. This allows for more concise and expressive array iteration. **Other considerations** * The benchmark assumes that the input array is not empty or null. * There is no consideration for cases where the array contains non-numeric elements, as this would cause different results between the two options. * The benchmark does not account for edge cases like very large arrays or arrays with sparse data. **Alternatives** Other alternatives to access the last element of an array could include: * Using `array[0]` followed by a loop that increments the index * Using a library like Lodash's `tail()` function, which provides a more concise and expressive way to access the tail of an array. * Using a JavaScript loop that iterates over the array using `for (let i = 0; i < array.length - 1; i++)` It's worth noting that the "at" method is generally considered a better option for accessing array elements in modern JavaScript development due to its conciseness and expressiveness. However, older browsers or versions may not support this method, making direct indexing a safer choice in those cases.
Related benchmarks:
array[array.length - 1] vs array.at(-1)
array[0] vs array.at(0)
array[1] vs array.at(1)
array[index] vs array.at(index)
Comments
Confirm delete:
Do you really want to delete benchmark?