Last array element

Benchmark created on


Description

In this test, different ways to access the last element of an array are analyzed. The test checks the performance of accessing the last element of a large array with a fallback to null if the array is empty.

Preparation HTML


Setup

const nodes = Array.from({ length: 100000 }, () => Math.random());

Test runner

Ready to run.

Testing in
TestOps/sec
if
const last = nodes[nodes.length - 1];

if(last) {
  return last;
}

return  null;
ready
short if
const last = nodes[nodes.length - 1];
return last ? last : null;
ready
null coalescing
return nodes[nodes.length - 1] ?? null;
ready

Revisions

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