Nullish coalescing vs Optional chaning

Benchmark created on


Setup

function getNull() {
  return null
}

function getObject() {
  return { nestedValue: 0 }
}

Test runner

Ready to run.

Testing in
TestOps/sec
Get object value by Nullish coalescing
const { nestedValue } = getObject() ?? {}
ready
Get object value by Optional chaning
const nestedValue = getObject()?.nestedValue
ready
Get null by Nullish coalescing
const { nestedValue } = getNull() ?? {}
ready
Get null by Optional chaning
const nestedValue = getNull()?.nestedValue
ready

Revisions

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