0

Condition 1

for( var i = 0; i < 1000; i++ ) {
    if( i != 0 ) {
        console.log("i is not zero.!");
    } else {
        console.log("i is zero.!");
    }
}

Condition 2

for( var i = 0; i < 1000; i++ ) {
    if( i == 0 ) {
        console.log("i is zero.!");     
    } else {
        console.log("i is not zero.!");
    }
}
  1. Which method is preferable ( I mean more appropriate ).?

  2. Is there any performance difference between those two.?

Sark
  • 4,458
  • 5
  • 26
  • 25
  • 6
    None. Do the zero thing outside of the loop, and then start the loop at 1. – some May 12 '16 at 07:42
  • 1
    There is no performance difference in either of these statements – Liam May 12 '16 at 07:44
  • 1
    http://c2.com/cgi/wiki?PrematureOptimization – Bálint May 12 '16 at 07:44
  • In Condition 2, the IF will fail for 999 times and switch to ELSE block. but in Condition 1 the IF will succeed for 999 times and won't switch to ELSE block. doesn't make it any difference.??? – Sark May 12 '16 at 07:55
  • 1
    It is not as simple as that. It might look like that, but what happens behind the scenes depends on the javascript engine. You might also want to read about branch prediction in this answer: http://stackoverflow.com/a/11227902/36866 – some May 12 '16 at 08:03
  • This could by written: `console.log(i==0?"i is zero.!":"i is not zero.!")` or `console.log(i!=0?"i is not zero.!":"i is zero.!")` – F. Hauri - Give Up GitHub May 12 '16 at 09:03

5 Answers5

1

Try it!

There is another way to do same:

console.log( i==0 ? "i is zero.!" : ""i is not zero.! );

There is a little bench:

function run(e) {
  var start=1*new Date();
  var loop=loopcnt.value;
  if (e.target.id=="==") {
    for( var i = 0; i < loop; i++ ) {
      if( i == 0 ) {
        var log="i is zero.!";       
      } else {
        var log="i is not zero.!";
      }
    }
  } else if (e.target.id=="!=") {
    for( var i = 0; i < loop; i++ ) {
      if( i != 0 ) {
        var log="i is not zero.!";       
      } else {
        var log="i is zero.!";
      }
    }
  } else if (e.target.id=="(=") {
    for( var i = 0; i < loop; i++ ) {
      var log=i==0?"i is zero.!":"i is not zero.!";
    }      
  } else {
    for( var i = 0; i < loop; i++ ) {
      var log=i!=0?"i is not zero.!":"i is zero.!";
    }      
  };
  elapsed.innerHTML=(1*new Date()-start).toFixed(0)+"ms."
}
body,button,input{font-family:sans;font-size:8pt;padding:0pt}div{font-size:12pt;}
Loop: <input id=loopcnt value="3000000" size="8" />
<button id="==" onclick="run(event)">if == {} else {}</button>
<button id="!=" onclick="run(event)">if != {} else {}</button>
<button id="(=" onclick="run(event)">( == ? "" : "" )</button>
<button id="(!" onclick="run(event)">( != ? "" : "" )</button>
<div id="elapsed"></div>

By using firefox or chrome, there are no sensible difference, but Spidermonkey seem doing thing a little quicker by using != than by using ==:

time smjs <<<'
    for( var i = 0; i < 10000000; i++ ) {
        if ( i == 0 ) { var log="i is zero.!"; }
        else { var log="i is not zero.!"; } } '
real    0m0.054s
user    0m0.044s
sys     0m0.004s

time smjs <<<'
    for( var i = 0; i < 10000000; i++ ) {
       if ( i != 0 ) { var log="i is not zero.!"; }
       else { var log="i is zero.!"; } } '
real    0m0.043s
user    0m0.040s
sys     0m0.000s

time smjs <<<'
for( var i = 0; i < 10000000; i++ ) {
       var log=(i==0?"i is zero.!":"i is not zero.!"); } '
real    0m0.051s
user    0m0.048s
sys     0m0.000s


time smjs <<<'
    for( var i = 0; i < 10000000; i++ ) {
       var log=(i!=0?"i is not zero.!":"i is zero.!"); } '
real    0m0.049s
user    0m0.040s
sys     0m0.008s

But this is not well tested (on really free host, by doing a lot of tests and full stats).

F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
0

Both the methods will produce same result. There is no any performance and efficiency difference between this two methods. You can use any one of them.

Raj Khot
  • 102
  • 1
  • 17
0

In both of your Condition 1 and Condition 2 the output will be

i is zero.!

and followed by 999 times

i is not zero.!
  1. It wouldnt matter which of the two conditions you use it will remain same.
  2. No, there wont be any performance difference as the no. of iterations performed remains the same
pradyot
  • 174
  • 12
0

You probably heard about how processors predict if a jump is made or not in order to compute several dependent operations at the same time. This is relevant in some contexts, but in yours, no matter in which order you do these, the processor will predict correctly.

In any case, such things shave of nanoseconds at best. You do such considerations when it's about core mechanisms, not for regular applications. I am not even sure if something like this would work with JavaScript anyways, might be not, since it's interpreted.

Most likely, for your program, it will be far more important to keep up with things such as readability.

Aziuth
  • 3,652
  • 3
  • 18
  • 36
  • I agree, but if either IF or ELSE block contains some heavy processing tasks, it still make the difference right.? – Sark May 12 '16 at 07:59
  • No, it wouldn't. Again, the processor would correctly predict. – Aziuth May 12 '16 at 08:08
0

For example if you are using a sparse matrix and you don't know the values but know the most items have zero value it is better to use condition 2.

If you can't estimate zero counts it is better to use :

        for( var i = 0; i < 1000; i++ ) {
                bool a = i;
                switch(a){
                case true:
                     console.log("i is not zero");
                     break;
                case false:
                     console.log("i is zero");
                      break;
                     }

        }