Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array[0] vs array.at(0) on 100000 elements
(version: 0)
Comparing performance of:
1 vs 2
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [...Array(100000).keys()]
Tests:
1
var d = data[data.length - 1]
2
var z = data.at(-1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
1
2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
1
150159952.0 Ops/sec
2
143091344.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark test case, which compares two approaches for accessing the last element of an array in JavaScript: using square bracket notation (`array[0]`) versus `Array.prototype.at(0)`. **Benchmark Definition** The benchmark definition is: * **Script Preparation Code:** "var data = [...Array(100000).keys()]" * This code creates a large array with 100,000 elements and assigns it to the variable `data`. The keys of this array are generated using the spread operator (`...`) and the `Array` constructor. * **Html Preparation Code:** null * There is no HTML preparation code provided, which means that the benchmark only runs in a JavaScript environment. **Individual Test Cases** The benchmark consists of two test cases: 1. **Test Case 1:** * **Benchmark Definition:** "var d = data[data.length - 1]" * This line accesses the last element of the `data` array using square bracket notation (`array[0]`). 2. **Test Case 2:** * **Benchmark Definition:** "var z = data.at(-1)" * This line attempts to access the last element of the `data` array using the `Array.prototype.at()` method. **Pros and Cons** * **Square Bracket Notation (`array[0]`):** * Pros: * Wide support across older browsers and environments. * Easy to read and understand. * Cons: * Can be slower than the `Array.prototype.at()` method due to its nature of accessing elements at a specific index, even if the array is not fully populated. * **`Array.prototype.at(0)` Method:** * Pros: * Generally faster and more efficient than square bracket notation for large arrays. * Provides a concise way to access the last element of an array without having to calculate the index manually. * Cons: * Limited support across older browsers and environments. (Added in ECMAScript 2019) * Requires modern JavaScript engines to execute. **Library/Functionality** * `Array.prototype.at()` Method: + This method is a part of the ECMAScript standard, introduced in ECMAScript 2019. + It provides an efficient way to access the element at a specific index in an array, even if the array is not fully populated. **Special JS Feature/Syntax** * No special JavaScript features or syntax are used in this benchmark. **Other Alternatives** For accessing the last element of an array, other alternatives include: * Using `Math.max()` and the spread operator (`...`): `var d = Math.max(...data)` * Using a loop to iterate through the elements of the array: `var d; for (const i of data) { d = i; break; }` However, these alternatives may not be as efficient or concise as using square bracket notation (`array[0]`) or the `Array.prototype.at()` method. **Benchmark Preparation Code** The provided benchmark preparation code creates a large array with 100,000 elements and assigns it to the variable `data`. This ensures that both test cases have access to a large dataset for comparison.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
array[0] vs array.at(0)
array[1] vs array.at(1) 2
array[index] vs array.at(index)
Comments
Confirm delete:
Do you really want to delete benchmark?