| The reason why the wicked is so fast in Firefox is because of the literal string and the optimizations done by the compiler. Try this (creates a function and then show the source): Function('return ~~(1 * "12");').toString();
And you get this result: "function anonymous() { return 12; }"
Notice that the expression has been converted to 12 in the function, so each call to it will just return 12 instead of converting it. That's why it is so much faster. The same is true for "<<", ">>" and "1 * '12'"I created another test where it actually do the conversion, by assigning the string to a variable and use that in each test. There parseInt is faster in Firefox, Opera, Chrome and Safari. Surprisingly it is the slowest in IE, where +str is the fastest. You can find it here:
http://jsperf.com/parse-number-from-string Also notice: * All the logical operators (~, <<, >>, |) operates internally on a 32 bit signed integer, while parseInt, Number and + operates on a 64 bit signed float. Try to convert 2147483648 or 2333444555 for a little surprise. * At the other jsperf someone added "0+'12'". That returns a string, not a number. |