Get last item in array (v3)

Revision 3 of this benchmark created on


Description

Which is better for getting the last item in array: square brackets or .at()?

Setup

function generateRandomWord(length) {
    let result = '';
    const characters = 'abcdefghijklmnopqrstuvwxyz';
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    return result;
}

const testLen = 1000;

const words = Array.from({ length: testLen }, () => generateRandomWord(5));

Test runner

Ready to run.

Testing in
TestOps/sec
square brackets []
const lastWord = words[words.length-1];
ready
at()
const lastWord = words.at(-1);
ready

Revisions

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