Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get last element of array2
(version: 0)
Comparing performance of:
length vs at vs slice
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(100000000).fill(0);
Tests:
length
var a = arr[arr.length - 1];
at
var b = arr.at(-1);
slice
var c = arr.slice(-1)[0]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
length
at
slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
length
5251306.5 Ops/sec
at
9310570.0 Ops/sec
slice
6900354.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the provided JSON and benchmark results. **Benchmark Definition** The benchmark definition is to measure the performance of different methods to get the last element of an array containing 100 million elements. **Individual Test Cases** There are three test cases: 1. **length**: `var a = arr[arr.length - 1];` * This method uses the built-in `length` property of arrays and subtracts 1 from it to access the last element. 2. **at**: `var b = arr.at(-1);` * This method uses the modern `at()` method, which allows accessing elements by their index. The `-1` argument means "the last element". 3. **slice**: `var c = arr.slice(-1)[0];` * This method uses the `slice()` method to create a new array with only the last element of the original array. **Library and Special JS Features** There is no external library used in these test cases. However, the **at()** method is a modern JavaScript feature introduced in ECMAScript 2020 (ES2020). It allows accessing elements by their index using the `at(index)` syntax. The other two methods (**length** and **slice**) are classic JavaScript methods. **Benchmark Results** The latest benchmark results show the performance of each test case on Chrome 125, a desktop browser running on Mac OS X 10.15.7: * **at**: ~9,310,570 executions per second * **slice**: ~6,900,354 executions per second * **length**: ~5,251,306 executions per second **Options Compared** The three test cases compare different methods to access the last element of an array: * Using the built-in `length` property (`var a = arr[arr.length - 1];`) * Using the modern `at()` method (`var b = arr.at(-1);`) * Using the `slice()` method with negative indexing (`var c = arr.slice(-1)[0];`) **Pros and Cons** Here are some pros and cons for each approach: * **length**: Pros: simple, classic JavaScript syntax. Cons: might be slower than modern methods. * **at**: Pros: modern syntax, efficient. Cons: only supported in newer browsers (ES2020+). * **slice**: Pros: can also create a new array with only the last element, if needed. Cons: might be slower than other two methods. **Other Alternatives** Other alternatives to get the last element of an array include: * Using `pop()`: `arr.pop();` (removes and returns the last element) * Using a simple indexing approach: `var d = arr[99];` (assuming an array of length 100 million) Keep in mind that these approaches might have different performance characteristics, depending on the specific use case.
Related benchmarks:
Array clone
array last element big data
shallow copy of 6M elements array
Clone Array - 08/02/2024
Comments
Confirm delete:
Do you really want to delete benchmark?