Null coalescing vs OR operator for out of bound array access

Benchmark created on


Description

Testing the performance difference between null coalescing operator and OR when accessing an out of bounds array position.

Preparation HTML

<script>
let arr = [1, undefined, 3, undefined];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Null coalesing
let sum = 0;
for (let i = 0; i <= 1000; i++)
  sum += arr[i % arr.length] ?? 0;
ready
OR operator
let sum = 0;
for (let i = 0; i <= 1000; i++)
  sum += arr[i % arr.length] || 0;
ready

Revisions

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