Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array last item
(version: 0)
Comparing performance of:
Spread, reverse, destructure vs Access by length - 1
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Spread, reverse, destructure
const items = new Array(5000).fill(null).map((_, i) => i); for(let i=0; i<10000; ++i){ let [last] = [...items].reverse(); }
Access by length - 1
const items = new Array(5000).fill(null).map((_, i) => i); for(let i=0; i<10000; ++i){ let last = items[items.length - 1]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread, reverse, destructure
Access by length - 1
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark is designed to measure the performance of accessing an element at the end of an array in JavaScript. The test creates two different scenarios: 1. "Access by length - 1": This test accesses the last element of an array using indexing (e.g., `items[items.length - 1]`). 2. "Spread, reverse, destructure": This test uses a more complex approach: it first reverses the array and then destructures the last element using the spread operator (e.g., `[...items].reverse(); let [last] = [...items];`). **Options Compared** The benchmark compares two different approaches to accessing an element at the end of an array: 1. **Indexing**: Using the `[]` notation to access the last element of an array. 2. **Spread and reverse**: Reversing the array using the spread operator (`[...items]`) and then destructuring the last element. **Pros and Cons** * **Indexing (Access by length - 1)**: + Pros: Simple, efficient, and widely supported. + Cons: May not be as readable or maintainable, especially for larger arrays. * **Spread and reverse (Spread, reverse, destructure)**: + Pros: Can be more expressive and readable, especially for complex operations. + Cons: May be slower due to the additional operations involved (reversing the array). **Other Considerations** In addition to these two approaches, other methods can also be used to access an element at the end of an array, such as: * Using `Array.prototype.at()` (if supported by the browser) * Creating a custom function or method for accessing the last element However, these alternatives are not explicitly tested in this benchmark. **Library Usage** None of the test cases use any external libraries. The tests rely solely on native JavaScript features. **Special JS Features or Syntax** The benchmark uses several advanced JavaScript features: * Destructuring assignment (e.g., `let [last] = [...items];`) * Spread operator (`[...items]`) * Reverse iteration using `[...items].reverse()`
Related benchmarks:
last array element
array.prototype.at() vs array[array.length - 1]
last item in array [length-1] vs slice(-1) vs pop
Slice vs Index
`array.slice(-1)[0]` vs `array[array.length - 1]`
Comments
Confirm delete:
Do you really want to delete benchmark?