Property check vs. null propagation

Benchmark created on


Setup

const definitions = [
  { width: 50 },
  { width: 100 },
  { minWidth: 5 },
  { minWidth: 7 },
  {},
  {}
];

Test runner

Ready to run.

Testing in
TestOps/sec
Property check and addition
let total = 0;
for(const definition of definitions) {
	if('width' in definition) {
	  total += definition.width ?? 0;
	} else {
	  total += definition.minWidth ?? 0;
	}
}
ready
Null propagation addition
let total = 0;
for(const definition of definitions) {
	total += definition.width ?? definition.minWidth ?? 0;
}
ready

Revisions

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