Ternary vs If-Else vs If (v13)

Revision 13 of this benchmark created on


Description

Testing which is faster ternary, if-else, or just if.

Setup

var a,
        b,
        c,
        d,
        e = 3;
    a = Math.floor(Math.random() * 2);
    b = Math.floor(Math.random() * 2);
    c = Math.floor(Math.random() * 2);
    d = Math.floor(Math.random() * 2);

Teardown


    a = Math.floor(Math.random() * 2);
    b = Math.floor(Math.random() * 2);
    c = Math.floor(Math.random() * 2);
    d = Math.floor(Math.random() * 2);
  

Test runner

Ready to run.

Testing in
TestOps/sec
Ternary
if (a ? b === c : d) {
        e = 4;
}
return e;
ready
if-else
if (a) {
        if (b === c) {
                e = 4;
        }
} else if (d) {
        e = 4;
}
return e;
ready
If
if (a || d) {
        if (b === c || (!a && d)) {
                e = 4;
        }
}
return e;
ready

Revisions

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