Last Array Element

Compact view

Benchmark publishedon

Description

Benchmarking ways to grab the last element of an array

Setup

const arr = Array.from({ length: 1_000_000 }, (_, i) => i);

Test Runner

Initializing...

Testing in
Test CaseOps/sec
In-place reverse + rest destructuring
const [a, ...rest] = arr.reverse();
globalThis.result = a;
ready
Slice to one-element array
globalThis.result = arr.slice(-1);
ready
Direct indexing
globalThis.result = arr[arr.length - 1];
ready
Negative index with at()
globalThis.result = arr.at(-1);
ready
Slice last element
globalThis.result = arr.slice(-1)[0];
ready
Slice copy + pop
globalThis.result = arr.slice().pop();
ready
Spread copy + pop
globalThis.result = [...arr].pop();
ready
Spread copy + reverse
globalThis.result = [...arr].reverse()[0];
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

Revision 1
publishedon
Revision 2
publishedon