Hacker News new | past | comments | ask | show | jobs | submit login
Trivial JavaScript: (a || b) vs (a && b) (samholmes.me)
1 point by samholmes on April 4, 2013 | hide | past | favorite | 1 comment



I couldn't understand how !a&&!b would be slower; the NOT should be included in the branch.

The Firefox JIT inspector suggested that the body of the loop was being entirely removed, which makes sense, because it doesn't do anything. A body more like this does the trick:

    if(!a&&!b) {tt+=1;} else {tf+=1;}
You'll need to add the relevant declarations to the setup section.

    var tt=0,tf=0;
(The assignment to a and b should probably go there, too - it's in the teardown section at the moment.)

The result, though, is still the same!

I fixed this by switching the bodies round, so that !a&&!b runs first, then !(a||b) runs second. Now !a&&!b is quicker, as it should be, and I feel vindicated. Sort of.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: