Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.slice(-1) vs Array.At(-1) Teste Pro Eduzeira
(version: 0)
Array.slice(-1) vs Array.At(-1)
Comparing performance of:
array.at(-1); vs array.slice(-1);
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const array = [1,2,3,4,5,6,7,8,9,10,11,12,13];
Tests:
array.at(-1);
const array = [1,2,3,4,5,6,7,8,9,10,11,12,13]; array.at(-1);
array.slice(-1);
const array = [1,2,3,4,5,6,7,8,9,10,11,12,13]; array.slice(-1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.at(-1);
array.slice(-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/126.0.0.0 Safari/537.36 OPR/112.0.0.0 (Edition Yx 05)
Browser/OS:
Opera 112 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.at(-1);
221482864.0 Ops/sec
array.slice(-1);
65879152.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Definition** The benchmark is comparing two approaches to access the last element of an array in JavaScript: `Array.at(-1)` and `Array.slice(-1)`. The purpose of this benchmark is to determine which approach is faster on a specific test case, as specified in the "Script Preparation Code". **Options Compared** There are only two options being compared: 1. **`Array.at(-1)`**: This method uses the `at()` method to access an element at a specified index. In this case, the negative of the index `-1` is used to get the last element. 2. **`Array.slice(-1)`**: This method returns a new array containing the last element of the original array. **Pros and Cons** * **`Array.at(-1)`** * Pros: * More concise and expressive syntax compared to `slice()`. * Returns the specified value if the index is out of range, avoiding potential errors. * Cons: * Not supported in older browsers or environments that don't support modern JavaScript features. * **`Array.slice(-1)`** * Pros: * Supported in all versions of JavaScript and most browsers. * Allows for more control over the resulting array (e.g., using `slice()` to get a subset). * Cons: * Returns an empty array if the input is null or undefined, which might not be desirable behavior. **Library and Purpose** None are explicitly mentioned in this benchmark. **Special JS Features or Syntax** The benchmark uses modern JavaScript features such as: * `const` (block scope declaration) * Template literals (`\r\n`) * Arrow functions (`=>`) (although not used here, it's implied) These features are part of the ECMAScript standard and are widely supported in modern browsers. **Other Alternatives** If you want to compare performance with other methods to access the last element of an array: * **`array[array.length - 1]`**: This uses direct indexing on the array, which is equivalent to `Array.slice(-1)`. However, it's generally considered less readable and might not be as efficient in some cases. * **`Object.keys(array)[-1]`**: This uses `Object.keys()` to get an array of keys for the object (which is an array), and then takes the last key using indexing. While this works, it's a more complex and slower approach. For this specific benchmark, only two approaches (`Array.at(-1)` and `Array.slice(-1)`) are being compared.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]`
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
Array clone from index 1 to end: spread operator vs slice
at(-1) vs slice(-1)[0] vs length - 1
Comments
Confirm delete:
Do you really want to delete benchmark?