简体   繁体   English

为什么parseInt()比Firefox中的* 1慢得多?

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

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 . 但我需要它作为一个数字,所以我做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: 然后我想“嗯,我应该只使用parseInt() 。然后我运行了一些jsperf测试,并且Firefox中的结果很有趣:

http://jsperf.com/parseintx1 http://jsperf.com/parseintx1

Across the board, it appears the operations are pretty similar, except in Firefox, using *1 is exceptionally faster. 总体而言,看起来操作非常相似,除了在Firefox中,使用*1非常快。 What's going on here? 这里发生了什么?


Edit 编辑

Someone made the base 10 test, and updated the tests overall. 有人做了基础10测试,并整体更新了测试。 Click away on this one too to give some extra feedback: http://jsperf.com/parseintx1/2 点击此处也可以提供一些额外的反馈: http//jsperf.com/parseintx1/2

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: 我不是一个JavaScript引擎专家,甚至不是编译器专家,但我很确定它归结为编译器可以告诉你:

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. 因为“a”是一个局部变量,并且从初始化到* 1表达式时未被使用,所以根本不需要生成代码来执行操作。 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 ; 在那之后,编译器也可以告诉“a”没有办法从函数中“逃脱”,所以没有必要做任何事情 ; that is, it may be that the * 1 test ends up with something equivalent to what you'd get from: 也就是说, * 1测试最终会得到与你得到的相同的东西:

function() {}

In the parseInt() case, however, the compiler can't be sure that parseInt() is really parseInt() , as it may have been redefined. 但是,在parseInt()情况下,编译器无法确定parseInt()是否真的是parseInt() ,因为它可能已被重新定义。 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. 它必须是测试设置,因为此版本也在Firefox中提供了预期的结果。 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. 我认为,你的设置中的事情是,每次迭代都会将parseInt应用(至少在FF中)到每个变量,而从StringNumber的转换可以在乘法测试的第一次迭代中应用,之后变量是数字,乘法不再需要转换。

In version 7 the variables are assigned in the test setup, and the test assigns new variables at every iteration. 在版本7中,变量在测试设置中分配,测试在每次迭代时分配新变量。 Now both tests have 'equal changes', and parseInt outperforms the multiplication test. 现在两个测试都有“相同的变化”,并且parseInt优于乘法测试。

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. 在IE [8,9]中检查测试结果并看到其结果与FF的结果相似之后,我认为Chrome结果有一个解释:我非常确定Chrome / Webkit在第一版测试中有更好的优化(特别是parseInt部分),它为那里的parseInt提供了更好的结果。 It may be the precompilation of (parts of) the code in the V8 engine, used in those browsers. 它可能是V8引擎中代码(部分)的预编译,在这些浏览器中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM