Reputation: 4546
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.!");
}
}
Which method is preferable ( I mean more appropriate ).?
Is there any performance difference between those two.?
Upvotes: 0
Views: 102
Reputation: 70792
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).
Upvotes: 1
Reputation: 102
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;
}
}
Upvotes: 0
Reputation: 3902
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.
Upvotes: 0
Reputation: 174
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.!
Upvotes: 0
Reputation: 102
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.
Upvotes: 0