Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array[array.length - 1] vs array.at(-1) (V2)
(version: 0)
Comparing performance of:
array[array.length - 1] vs array.at(-1)
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (let i = 0; i < 1000; ++i) { array[i] = i; } var results = [];
Tests:
array[array.length - 1]
results.push(array[array.length - 1]);
array.at(-1)
results.push(array.at(-1));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array[array.length - 1]
array.at(-1)
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):
Let's break down the provided benchmark definition and test cases. **What is being tested?** MeasureThat.net is testing two different ways to access the last element of an array in JavaScript: `array[array.length - 1]` (also known as indexing) and `array.at(-1)` (introduced in ECMAScript 2019). The goal is to compare their performance. **Options being compared** The benchmark compares two approaches: 1. **Indexing (`array[array.length - 1]`)**: This method involves accessing the last element of an array by its index, which is calculated as `length - 1`. This approach has been used for a long time in JavaScript. 2. **Array.prototype.at() (`array.at(-1)`)**: This method was introduced in ECMAScript 2019 and provides a more concise way to access the last element of an array. It uses the `at()` function, which takes two arguments: an index (in this case, `-1`) and a options object. **Pros and cons** Here are some pros and cons for each approach: **Indexing (`array[array.length - 1]`)** Pros: * Wide support across browsers and versions * Familiar to most developers who have used JavaScript for a long time Cons: * Can be slower than the new `at()` method due to additional overhead (e.g., checking if the array has at least one element) * More verbose syntax (`array[length - 1]` instead of just `array.at(-1)`) **Array.prototype.at() (`array.at(-1)`)** Pros: * Faster performance compared to indexing * More concise syntax Cons: * Less supported across browsers and versions (only available in ECMAScript 2019 and later) * May require additional imports or module loading if not already available **Other considerations** The benchmark also uses a common approach for measuring performance: creating an array of 1000 elements, filling it with numbers from 0 to 999, and then pushing the results of both indexing methods onto an array. **Library usage (none)** There are no libraries used in this benchmark. **Special JS features or syntax** The `at()` method is a new JavaScript feature introduced in ECMAScript 2019. It provides a more concise way to access elements in arrays, but its availability can vary across browsers and versions. In summary, the benchmark compares the performance of two approaches for accessing the last element of an array: indexing (`array[array.length - 1]`) and the new `at()` method (`array.at(-1)`). The pros and cons of each approach are discussed, along with some considerations for implementing this benchmark.
Related benchmarks:
array[array.length - 1] vs array.at(-1)
array[1] vs array.at(1)
array[array.length - 1] vs array.at(-1) (V3)
at(-1) vs slice(-1)[0] vs length - 1
array[index] vs array.at(index)
Comments
Confirm delete:
Do you really want to delete benchmark?