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 return true || 1

http://jsperf.com/or-condition2 var condition = true || 1; return condition; 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

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.


true || 1 return true, it still need to check true || 1 with next coming 1(recursively). so I guess that's the reason.

链接地址: http://www.djcxy.com/p/64164.html

上一篇: Javascript原型vs $ .extend

下一篇: JS:为什么这会更慢? 它不应该测试其他或条件,但它呢?