Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
`array[i]` vs `array.at(i)`
(version: 9)
Comparing performance of:
array[i] vs array.at(i)
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [1,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3] var N = array.length
Tests:
array[i]
let sum = 0 for (let i = 0; i < N; i++) sum += array[i]
array.at(i)
let sum = 0 for (let i = 0; i < N; i++) sum += array.at(i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array[i]
array.at(i)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array[i]
262712.5 Ops/sec
array.at(i)
259925.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the benchmark. **What is being tested?** MeasureThat.net is comparing two approaches for accessing elements in an array: `array[i]` and `array.at(i)`. The test case uses an array of 30 elements, where each element appears multiple times. **Options compared:** 1. **`array[i]`:** This approach uses the traditional array indexing syntax, where you specify the index of the element you want to access using square brackets (`[]`). For example, `array[0]`, `array[10]`, etc. 2. **`array.at(i)`:** This approach uses the `at()` method, which is a relatively new feature introduced in ECMAScript 2019 (ES2020). The `at()` method returns the element at the specified index, and also provides additional features like handling out-of-bounds indices and more. **Pros and Cons:** 1. **`array[i]`:** * Pros: + Widely supported in older browsers and environments. + Generally faster execution speed. * Cons: + More code to write, as you need to specify the index explicitly. + Can lead to errors if the array length changes unexpectedly. 2. **`array.at(i)`:** * Pros: + More concise and readable code. + Handles out-of-bounds indices more elegantly, reducing errors. + May be faster in newer browsers and environments. * Cons: + Less widely supported in older browsers and environments (not yet adopted by all major browsers). + May have performance overhead due to the additional method call. **Library used:** In this test case, there is no specific library being used. The array data and the benchmarking logic are defined directly in the JavaScript code. **Special JS feature or syntax:** The `at()` method is a relatively new feature introduced in ECMAScript 2019 (ES2020). It was added to provide a more modern and expressive way of accessing elements in arrays, while also handling edge cases like out-of-bounds indices. **Other alternatives:** If you were to use alternative approaches for accessing array elements, some options might include: * Using `Array.prototype.indexOf()` or `Array.prototype.findIndex()` to find the index of an element, then using indexing (`array[index]`) to access it. * Using a library like Lodash, which provides utility functions for working with arrays and other data structures. However, in this specific test case, the comparison is between `array[i]` and `array.at(i)`, so these alternatives are not relevant.
Related benchmarks:
array[array.length - 1] vs array.at(-1) 3
array[array.length - 1] vs array.at(-1) 4
array[1] vs array.at(1)
array[1] vs array.at(1) 2
array[index] vs array.at(index)
Comments
Confirm delete:
Do you really want to delete benchmark?