array length vs slice vs at

Benchmark created on


Description

return specific item from array

Setup

// Our array with items
const colors = ["red", "green", "blue"];

Test runner

Ready to run.

Testing in
TestOps/sec
length
// Using length property
const lengthWay = colors[colors.length - 2];
console.log(lengthWay); // 'green'
ready
slice()
// Using slice() method. Note an array is returned
const sliceWay = colors.slice(-2, -1);
console.log(sliceWay[0]); // 'green'
ready
at()
// Using at() method
const atWay = colors.at(-2);
console.log(atWay); // 'green'
ready

Revisions

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