Search code examples
javascriptprogramming-languages

Why would Safari offer nearly opposite results here?


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)


Solution

  • 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.