22 January 2011

Is really more faster ternary operator in JavaScript?

The Code:
function doTest1() {
    var start = (new Date()).getTime();
    var flag = false;

    for (var i=0;i<=10000000;i++)
    {
        var v = (flag) ? "something" : "else";
    }
    var elapsed = (new Date()).getTime() - start;
    return elapsed;
}

function doTest2() {
    var start = (new Date()).getTime();
    var flag = false;

    for (var i=0;i<=10000000;i++)
    {
        if (flag) {
            var v = "something";
        } else {
            var v = "else";
        }
    }
    var elapsed = (new Date()).getTime() - start;
    return elapsed;
}

    var average1 = 0;
    var average2 = 0;
    var loops = 100;
    for (var i=0;i<=loops;i++) {
        var timeElapsed1=doTest1();
        var timeElapsed2=doTest2();
        console.log("time elapsed in test1 " + i + " " + timeElapsed1);
        console.log("time elapsed in test2 " + i + " " + timeElapsed2);
        average1+=timeElapsed1;
        average2+=timeElapsed2;
    }
    average1=average1/loops;
    console.log("average1: (ternary) in " + loops + " loops: "+ average1);
    average2=average2/loops;
    console.log("average2: (if) in " + loops + " loops: "+ average2);
And results in Google Chrome:
[...]
time elapsed in test2 97 118
time elapsed in test1 98 79
time elapsed in test2 98 76
time elapsed in test1 99 99
time elapsed in test2 99 121
time elapsed in test1 100 138
time elapsed in test2 100 132
average1: (ternary) in 100 loops: 121.06
average2: (if) in 100 loops: 128.64
The difference in Chrome is negligible and I think it not worth.

Now the same in FireFox:
[...]
time elapsed in test2 97 605
time elapsed in test1 98 845
time elapsed in test2 98 569
time elapsed in test1 99 849
time elapsed in test2 99 591
time elapsed in test1 100 862
time elapsed in test2 100 590
average1: (ternary) in 100 loops: 870.83
average2: (if) in 100 loops: 655.69

OMG! Not only is faster but it's terribly slower to use the ternary operator in firefox.

My advice: do not use ternary operator in javascript.


Posted at BinaryCell

2 comments:

  1. yes ternary operator is a bit slowler, but not so much

    http://jsperf.com/ternary-operator

    ReplyDelete
  2. I think that the reason is in ternary operator there is no branch prediction(https://en.wikipedia.org/wiki/Branch_predictor) so it should complete the execution of the all condition checking to choose what to do.

    The the other way (if) it can do branch prediction so while he is checking the condition he is executing a part of the next code (A) then it depends on the condition to see if it should rollback the code he just executed (A) or just continue it.

    BTW: it's me Meher :)

    ReplyDelete


Disclaimer: In no event shall the blog owner, be liable for any damages, including without limitation, special, indirect or consequential damages, or any damages, whatsoever resulting from access or use, or inability to access or use this Website or arising out of any materials, information, qualifications or recommendations on this Website.