Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS: Why Is This Slower? It Shouldn't Test Other OR Conditions But It Does?

I just tested something. I always thought that in a OR condition, once the computer/browser finds something true, it returns it and doesn't test the other conditions. I structured my code around this assumption.

However, I timed it and it appears that the long test takes x4 more times, any explanation for that?

Note: Tested in Google Chrome console.

JSPerf:

http://jsperf.com/or-condition return true || 1

http://jsperf.com/or-condition2 var condition = true || 1; return condition;

http://jsperf.com/or-condition3 if(true || 1) return true Seems relatively faster.

EDIT: I just found how that the amount of conditions after the true is not important. What matters is the length of the condition. Check http://jsperf.com/or-condition5.

My theory is that the browser splits the function into 2+ different memory zones because of its length. When it calls the function, it needs to get data from multiple memory zones instead of 1.

a = function(){
    return true ||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1||1;
}
b = function(){
    return true;
}
//#############################################

var start = Date.now();
for(var i = 0 ; i < 1000000 ; i++){
    a();
}   var end = Date.now();       
console.log(end-start);     //3075


var start = Date.now();
for(var i = 0 ; i < 1000000 ; i++){
    b();
}   var end = Date.now();       
console.log(end-start);     //776
like image 860
RainingChain Avatar asked Jul 03 '14 23:07

RainingChain


1 Answers

It takes the JS engine longer to analyze the line of code when the condition gets longer and more complicated.

I believe this JSPerf test can explain it well.

like image 154
cmonday Avatar answered Oct 22 '22 23:10

cmonday