ternary vs if

Benchmark created on


Setup

const condition1 = Math.random() < 0.5;

const condition2 = Math.random() < 0.5;

const condition3 = Math.random() < 0.5;

Test runner

Ready to run.

Testing in
TestOps/sec
ifs


let val = null;
if (condition1) {
   val = 1
}
if (condition2) {
   val = 2
}
if (condition2) {
   val = 3
}

return val;
ready
ternary
const val = condition1 ? 1 : condition2 ? 2 : condition3 ? 3 : null;

return val;
ready

Revisions

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