最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is parseInt() so much slower than *1 in Firefox? - Stack Overflow

programmeradmin2浏览0评论

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:

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:

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./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./parseintx1/2

Share Improve this question edited Aug 28, 2012 at 17:00 j08691 208k32 gold badges269 silver badges280 bronze badges asked Aug 28, 2012 at 16:50 hookedonwinterhookedonwinter 12.7k19 gold badges63 silver badges74 bronze badges 14
  • 1 Add , 10 to force base 10. (I don't have Firefox right now) – SLaks Commented Aug 28, 2012 at 16:52
  • 2 ParseInt was faster for me in both Chrome and Firefox. – j08691 Commented Aug 28, 2012 at 16:57
  • 1 @j08691: Seconded. Got the same result – Madara's Ghost Commented Aug 28, 2012 at 17:02
  • 2 @j08691 Really? Same test? Link? – hookedonwinter Commented Aug 28, 2012 at 17:03
  • 3 It must have something to do with the test itself. @ jsperf./tilde-against-parseint-and/2 parseInt outperfoms all (FF 14.01) – KooiInc Commented Aug 28, 2012 at 17:13
 |  Show 9 more ments

2 Answers 2

Reset to default 2

I'm not a JavaScript engine expert by any means, or even a piler expert, but I'm pretty sure that it boils down to the fact that the piler 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 piler 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 piler 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.

It has to be the test setup, since this version gives the expected result in Firefox too. The thing, in your setup I think, is that parseInt is applied (well, in FF at least) in every iteration to every variable, whereas the conversion from String to Number may be applied the first iteration in the multiplication test, after which the variables are numeric and multiplication needs no conversion anymore.

In version 7 the variables are assigned in the test setup, and the test assigns new variables at every iteration. Now both tests have 'equal changes', and parseInt outperforms the multiplication test.

After inspecting the test in IE[8,9] and seeing its results look like those of FF I think there's an explanation for the Chrome results: I'm pretty sure Chrome/Webkit has a better optimization in the first version of the test (especially the parseInt part), which gives a better result for parseInt there. It may be the prepilation of (parts of) the code in the V8 engine, used in those browsers.

发布评论

评论列表(0)

  1. 暂无评论