JQuery / javascript if operator speed

Considering:

var
isIE = $.browser.msie && !$.support.opacity, 
isIE6 = isIE && $.browser.version < 7;

Which will be faster:

if(isIE6){ doSomething(); }
else { doSomethingElse(); }

OR

if(!isIE6){ doSomethingElse(); }
else { doSomething(); }

Are they exactly the same as speed?

+3
source share
4 answers

Given this test for 1,000,000 iteration cycles, no difference.

var test = true;

var count = 1000000;
var stop, start = new Date();

while(count--) {
    if(test) ; // Change to !test
    else ;
}

stop = new Date();

alert(stop - start);

Tested in Firefox, Safari and IE8.

Other processes running on the system that run the test several times in each browser return the same total change in milliseconds, regardless !.

+4
source

, , ( ), ( , ), , , .

+1

The first will be faster, because it requires one more step (the! Operator launches the action separately from the if statement).

However, there will be no real difference.

+1
source

I would say that you are just checking this. Firebug has a profiler as well as one in IE8.

Grz, Kris.

0
source

Source: https://habr.com/ru/post/1750522/


All Articles