views:

59

answers:

2

Test here: http://jsperf.com/test-for-speed-of-various-conditionals

I'm interested if others are getting the same results, and what people might think of why the results vary (esp. w/ Safari) across browsers. Interesting is how democratically Firefox handles the various cases.

Please inform if there is something terribly wrong with my methodology :)

Firefox 3.6/Mac OSX 10.64: Switch = 824,352 Ops/sec (14% slower)
If/else = 530,062 (44% Slower, Slowest)
Hash/lazy = 968,035 (Fastest)
Hash/if/else = 963,765 (0% Slower)

Chrome 6.0.472.63/Mac OSX 10.64:
Switch = 10,220,039 Ops/sec (62% slower)
If/else = 7,744,284 (71% Slower, Slowest)
Hash/lazy = 27,130,039 (Fastest)
Hash/if/else = 25,297,370 (6% Slower)

Safari 5.0.2/Mac OSX 10.64:
Switch = 15,044,132 Ops/sec (Fastest)
If/else = 1,793,051 (88% Slower, Slowest)
Hash/lazy = 10,381,941 (30% Slower)
Hash/if/else = 11,119,576 (26% Slower)

Opera 10.10/Mac OSX 10.64:
Switch = 497,238 Ops/sec (32% Slower)
If/else = 250,904 (66% Slower, Slowest)
Hash/lazy = 740,520 (Fastest)
Hash/if/else = 634,424 (14% Slower)

MSIE 8.0/Windows NT:
Switch = 176,267 Ops/sec (60% Slower)
If/else = 124,783 (72% Slower, Slowest)
Hash/lazy = 447,421 (Fastest)
Hash/if/else = 442,736 (14% Slower)

+2  A: 

Javascript has a specification but it doesn't define implementation; it's up to the browser vendors to determine how to implement the spec (which also leads to plenty of cross-browser issues, though they're getting better about that lately). It's probable that the way the various browsers implement the various methods you're using differ.

Daniel Vandersluis
A: 

These are not really fair tests. The reason the straight lookup is faster is because every other test also does the exact same lookup on top of a conditional check. And there is no real reason to write code like:

if (t == 'e') c['e'];

This is an obvious violation of DRY (Don't Repeat Yourself), and you wont save any time using a string literal in the lookup as the contents of variable t still have to be fetched for the comparison.

Also

if (c[t]) c[t]();

Is just as lazy as

c[t] && c[t]();

As to why Safari provides such opposite results, I have no idea, as it is not logical that "operation a + operation b < operation a". It should just be noted that performance testing in Javascript is often unreliable as the Date object is limited to milliseconds and even then does not usually provide sufficient accuracy for useful results.

And finally, all else being equal, an object lookup is never faster than an if/else or switch statement, and could in fact be several orders of magnitude slower. Also the difference between if/else and switch should be negligible if it exists, and is of course highly dependent on the specific browser/interpreter.

MooGoo
Thanks everyone. Some other notes from two other tests, one with a smaller set, and one where truth condition exists for very first case. In the smaller set the results remained the same except for Firefox, which suddenly was much faster with switch(). When I sought the very first result ('a' instead of 'g'), predictably, switch were faster across all browsers, *but* lazy evaluation was the *slowest* of the hash techniques (It seems the second hash lookup is fastest w/ small sets): (1)http://jsperf.com/simpler-conditional-test; (2)http://jsperf.com/simpler-conditional-test-first-case
Sandro Pasquali