Why is parseInt() so much slower than *1 in Firefox?

firefox, javascript, performance

Solution

I'm not a JavaScript engine expert by any means, or even a compiler expert, but I'm pretty sure that it boils down to the fact that the compiler can tell that this:

var a = "123";
a = a * 1;

is really exactly the same as:

var a = 123;

Because "a" is a local variable, and is unused from the point of its initialization to that `* 1` expression, there's no point generating code to carry out the operation at all. After that point, the compiler may also be able to tell that there's no way that "a" can "escape" from the function, so there's really no point doing anything; that is, it may be that the `* 1` test ends up with something equivalent to what you'd get from:

function() {}

In the `parseInt()` case, however, the compiler can't be sure that `parseInt()` is really `parseInt()`, as it may have been redefined. Thus, it has to generate code to make the function call.

Problem

I have a value stored as a string, and I know it will always be a whole number. But I need it as a number, so I was doing `n = n * 1`. Then I thought "hmm, I should probably just use `parseInt()`. Then I ran some jsperf tests, and the results in Firefox were interesting: http://jsperf.com/parseintx1 Across the board, it appears the operations are pretty similar, except in Firefox, using `*1` is exceptionally faster. What's going on here? Edit Someone made the base 10 test, and updated the tests overall. Click away on this one too to give some extra feedback: http://jsperf.com/parseintx1/2

Original source