Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
at(-1) vs (length - 1)
(version: 0)
Comparing performance of:
length - 1 vs at(-1)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for(var i = 0; i < 100000; i++){array.push(Math.random());}
Tests:
length - 1
array[array.length - 1];
at(-1)
array.at(-1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
length - 1
at(-1)
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
length - 1
98919184.0 Ops/sec
at(-1)
95449264.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its options. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case, specifically measuring the performance difference between accessing array elements using two different methods: `at(-1)` and `length - 1`. **Script Preparation Code** The script preparation code creates an empty array with 100,000 random elements. This is done to ensure that both benchmarking methods access the entire array. ```javascript var array = []; for (var i = 0; i < 100000; i++) { array.push(Math.random()); } ``` **Benchmark Definition** The benchmark definition specifies two test cases: 1. `array[array.length - 1];` (length - 1) 2. `array.at(-1);` (at(-1)) These methods both access the last element of the array. **Library and Syntax** Neither of these methods uses a library, and there are no special JavaScript features or syntax used in this benchmark. **Options Compared** The two options compared are: * `length - 1`: This method accesses the last element by subtracting 1 from the length of the array. It is a simple and straightforward way to access an element at a specific index. * `at(-1)`: This method accesses the last element using the `at()` method, which was introduced in ECMAScript 2019 (ES10). The `at()` method allows you to specify the index of an array using the dot notation (`array.at(index)`). **Pros and Cons** Here are some pros and cons of each approach: * `length - 1`: + Pros: Widely supported, no dependency on modern browsers. + Cons: May not be as efficient or readable, especially for large arrays. * `at(-1)`: (Note: The `-1` indexing in the benchmark definition is a bit confusing. In modern JavaScript, you would typically use `array.at(-1)` without quotes.) + Pros: More readable and efficient than `length - 1`, especially for larger arrays. + Cons: Requires support for modern browsers and ECMAScript 2019 features. **Other Considerations** When choosing between these two methods, consider the following: * If you need to access elements at a specific index in an older browser or environment, `length - 1` might be a better choice. * If you're writing new code for modern browsers and want to take advantage of efficient indexing, `at(-1)` is likely the better option. **Alternatives** Other alternatives for accessing array elements include: * Using `slice()` with negative offset (e.g., `array.slice(array.length - 1)`) * Using `index` property on the `Array.prototype` object (not recommended) * Using a simple loop to iterate over the array and access the desired element
Related benchmarks:
Fill array with random integers
Array .push() vs .unshift() with random numbers
Pop vs length -1 vs at -1
Spread Operator VS Array.prototype.slice() VS Array.prototype.slice(0)
Comments
Confirm delete:
Do you really want to delete benchmark?