使用函数别名时的奇怪行为 [英] Weird behavoir while using function aliases

查看:43
本文介绍了使用函数别名时的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:


功能测试(){}

Test.F = function(){}

Test.F.FF = function(){}

Test.F.FF.FFF = function(){}

Test.F.FF.FFF。 FFFF = function(){}

// var alias = function(){};

var alias = Test.F.FF.FFF.FFFF ;


var date1 = new Date();

for(var index = 0; index< 100000; index ++)

Test.F.FF.FFF.FFFF();

// alias();

var date2 = new Date();

print(date2.getTime() - date1.getTime());

为什么当我使用Test.F.FF.FFF.FFFF()时,我得到大约100毫秒,当我

得到别名()我得到280ms左右?

这里发生了什么?这是我从未预料到的事情

我想你们两个也不是。别名可以更快地工作

然后在每次迭代时进行锁定,对吧?

如果我取消注释这一行:var alias = function(){},从280ms我开始/>
下降到265毫秒?


现在有人这么做的原因吗?它只发生在Firefox中(我的b $ b有1.5.0.6)。 IE和Opera的表现应该是他们应该的行为

解决方案

Daniel Rucareanu写道:

< snip>


为什么我使用Test.F.FF.FFF.FFFF()当我得到别名时我得到大约100ms和

()我得到大约280ms?

这里发生了什么?这是我永远不会预期的事情,我想你们两个也不是。别名是什么?b $ b工作得更快,然后在每次迭代时做锁定,对吧?

如果我取消注释这一行:var alias = function(){},从280ms开始/>
下降到265毫秒?



鉴于javascript date

对象的分辨率和精度相对较低,以及任务切换对处理器的影响,我会

不信任是因为测量时间短而得到的结果。


现在有人知道这个的原因吗?它只发生在Firefox中(我的b $ b有1.5.0.6)。 IE和Opera的行为应该是



如果效果是真实的,特别是Firefox然后询问人们

谁写了javascript引擎对于Firefox可能有帮助。他们: -


netscape.public.mozilla.jseng

Richard。


js日期对象的精度确实很低,但是当测量大于几百毫秒的持续时间时,它的价格就足够了。


此外,我不认为任务/线程切换有任何影响,

因为结果是不变的,无论我怎么运行这个

测试。我还将循环从100K迭代增加到1M并且

结果保持不变:使用别名要慢得多。

Richard Cornford写道:
< blockquote class =post_quotes>
Daniel Rucareanu写道:

< snip>


为什么我使用Test.F.FF.FFF.FFFF()当我得到别名时我得到大约100ms和

()我得到大约280ms?

这里发生了什么?这是我永远不会预期的事情,我想你们两个也不是。别名是什么?b $ b工作得更快,然后在每次迭代时做锁定,对吧?

如果我取消注释这一行:var alias = function(){},从280ms开始/>
下降到265毫秒?



鉴于javascript日期的分辨率和精度相对较低

对象,以及任务切换对处理器的影响,我会

不信任是因为测量时间短而得到的结果。


现在有人知道这个的原因吗?它只发生在Firefox中(我的b $ b有1.5.0.6)。 IE和Opera的行为应该是



如果效果真实,特别是Firefox然后询问人们

谁编写了javascript引擎对于Firefox可能有帮助。他们: -


netscape.public.mozilla.jseng


理查德。


Daniel Rucareanu写道:


为什么我使用Test.F.FF时.FFF.FFFF()我得到大约100毫秒,当

我得到别名()我得到大约280毫秒?



我得到的结果与你不同:
http://javascripttoolbox.com/temp/fu...peed_test.html


为我使用别名的速度几乎快了两倍在Firefox 2 beta 2中,

在FF 1.5中快10%-25%,在IE6中快15%-20%。

试试我的网址,看看你是否得到相同的结果。


在任何情况下,进行100,000次查询,总计只需要100-200ms

肯定会使这些结果无关紧要。


-

Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox .com


I have the following script:

function Test(){}
Test.F = function(){}
Test.F.FF = function(){}
Test.F.FF.FFF = function(){}
Test.F.FF.FFF.FFFF = function(){}

//var alias = function(){};
var alias = Test.F.FF.FFF.FFFF;

var date1 = new Date();
for (var index = 0; index < 100000; index++)
Test.F.FF.FFF.FFFF();
//alias();
var date2 = new Date();
print(date2.getTime() - date1.getTime());
Why is it when I use Test.F.FF.FFF.FFFF() I get around 100ms and when I
get alias() I get around 280ms?
What is going on here? This is something I would have never anticipated
and I think that neither of you as well. Aliases whould work faster
then doing lockups at each iteration, right?
Also if I uncomment this line: var alias = function(){}, from 280ms I
get a drop to 265 ms?

Does anybody now the reason for this? It only happends in Firefox (I
have 1.5.0.6). IE and Opera behaive as they should

解决方案

Daniel Rucareanu wrote:
<snip>

Why is it when I use Test.F.FF.FFF.FFFF() I get around 100ms and
when I get alias() I get around 280ms?
What is going on here? This is something I would have never
anticipated and I think that neither of you as well. Aliases whould
work faster then doing lockups at each iteration, right?
Also if I uncomment this line: var alias = function(){}, from 280ms I
get a drop to 265 ms?

Given the relatively low resolution and precision of javascript date
objects, and the influence of task switching on the processor, I would
not trust results from measurements of periods as short as this.

Does anybody now the reason for this? It only happends in Firefox (I
have 1.5.0.6). IE and Opera behaive as they should

If the effect is real, and particular to Firefox then asking the people
who wrote the javascript engine for Firefox may help. They:-

netscape.public.mozilla.jseng

Richard.


The precision of js date objects is indeed kind of low, but more the
enough when measuring durations larger than a few hundred milliseconds.

Also I don''t think that tasks / threading switching have any influence,
because the result are constant, no matter how may time I run this
test. I also increased the loop from 100K iterations to 1M and the
results stay the same: using the alias is a lot slower.
Richard Cornford wrote:

Daniel Rucareanu wrote:
<snip>

Why is it when I use Test.F.FF.FFF.FFFF() I get around 100ms and
when I get alias() I get around 280ms?
What is going on here? This is something I would have never
anticipated and I think that neither of you as well. Aliases whould
work faster then doing lockups at each iteration, right?
Also if I uncomment this line: var alias = function(){}, from 280ms I
get a drop to 265 ms?


Given the relatively low resolution and precision of javascript date
objects, and the influence of task switching on the processor, I would
not trust results from measurements of periods as short as this.

Does anybody now the reason for this? It only happends in Firefox (I
have 1.5.0.6). IE and Opera behaive as they should


If the effect is real, and particular to Firefox then asking the people
who wrote the javascript engine for Firefox may help. They:-

netscape.public.mozilla.jseng

Richard.


Daniel Rucareanu wrote:

Why is it when I use Test.F.FF.FFF.FFFF() I get around 100ms and when
I get alias() I get around 280ms?

I get different results than you:
http://javascripttoolbox.com/temp/fu...peed_test.html

Using alias for me is always almost twice as fast in Firefox 2 beta 2,
10%-25% faster in FF 1.5, and 15%-20% faster in IE6.
Try my url and see if you get the same results.

In any case, doing a lookup 100,000 times and only taking 100-200ms total
surely makes these results insignificant.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


这篇关于使用函数别名时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆