First array element

Benchmark created on


Description

This test benchmarks various methods to access the first element of an array and return null if the array is empty.

Setup

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

Test runner

Ready to run.

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

return null;
ready
if length
if(nodes.length) {
  return nodes[0];
}

return null;
ready
short if
return nodes[0] ? nodes[0] : null;
ready
short if length
return nodes.length ? nodes[0] : null;
ready
node ?? null
return nodes[0] ?? null;
ready

Revisions

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