nth array element

Benchmark created on


Description

This test measures the performance of different methods to safely access an element in an array, ensuring no undefined or out-of-bounds errors occur.

Setup

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

let index = -20;

if(index < 0) {
  index = nodes.length + index
}

Test runner

Ready to run.

Testing in
TestOps/sec
if
if(nodes[index]) {
  return nodes[index];
}

return  null;
ready
short if
return nodes[index] ? nodes[index] : null;
ready
null coalescing
return nodes[index] ?? null;
ready

Revisions

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