函数指针vs javascript中的直接调用 [英] function pointers vs direct calling in javascript

查看:54
本文介绍了函数指针vs javascript中的直接调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道调用函数指针的性能v / sa
正常函数调用javascript中的场景有

多次调用js到同一个函数。如果有人为此工作,请分享您的

想法。


谢谢,

Sampat。

Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.

Thanks,
Sampat.

推荐答案

Sampat在2007年11月20日下午6:24发表以下内容:
Sampat said the following on 11/20/2007 6:24 PM:



我想知道调用函数指针的性能v / sa
javascript中的普通函数调用在有多个调用的场景中有多个调用js到同一个功能。如果有人为此工作,请分享您的

想法。
Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.



你不需要测试就可以搞清楚,虽然测试会证明这一点。

函数指针是*总是*去慢一点它必须更慢

,因为它不仅仅是普通的函数调用。你告诉

浏览器执行此操作,然后执行此操作与做那个相对第一个将

*总是*慢一点。


-

兰迪

机会有利于准备好的思想

comp.lang.javascript常见问题 - http ://jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/


11月21日上午9:24,Sampat< sampatdi ... @ gmail.comwrote:
On Nov 21, 9:24 am, Sampat <sampatdi...@gmail.comwrote:



我想知道调用函数指针的性能v / sa

javascript中的正常函数调用,在js中有多个调用

到同一个函数。如果有人为此工作,请分享您的

想法。
Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.



我不知道你的意思是函数指针,javascript中没有这样的

术语。如果你的意思是分配对

a函数的引用更好,而不是每次都使用长查找链,那么

答案是肯定的。


例如如果你需要多次调用以下方法:


baseObj.childObj.method();

那么你可以为本地分配一个引用变量:


var x = baseObj.childObj.method;

x();


应该更快,但可能只有在相同的执行上下文中必须调用

方法两次以上。如果你打电话给函数

10,000次左右,你可能只能测量差价,用户可能不会发现差异直到

你称它为100,000次(左右)。


但是,如果你的意思是函数指针你在

调用函数中包装一个方法,如:


var x = function(){

baseObj.childObj。方法();

}

然后看看兰迪的答案。

-

Rob

I don''t know what you mean by "function pointer", there is no such
term in javascript. If you mean is it better to assign a reference to
a function rather than use a long lookup chain every time, then the
answer is yes.

e.g. If you need to call the following method a number of times:

baseObj.childObj.method();
then you can assign a reference to a local variable:

var x = baseObj.childObj.method;
x();

That should be faster, but probably only if you have to call the
method more than twice within the same execution context. You will
likely only be able to measure the difference if you call the function
10,000 times or so, and users likely won''t notice a difference until
you call it 100,000 times (or so).

However, if you mean by "function pointer" that you wrap a method in a
calling function like:

var x = function(){
baseObj.childObj.method();
}
then see Randy''s answer.
--
Rob


11月21日凌晨1点35分,Randy Webb< HikksNotAtH ... @ aol.comwrote:
On Nov 21, 1:35 am, Randy Webb <HikksNotAtH...@aol.comwrote:

Sampat在2007年11月20日下午6:24发表以下内容:
Sampat said the following on 11/20/2007 6:24 PM:



我想知道通话的表现一个函数指针v / sa

javascript中的正常函数调用,在js中有多个调用

到同一个函数。如果有人为此工作,请分享您的

想法。
Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.



你不需要测试来解决它,虽然测试会证明它。

函数指针是*总是*去慢一点它必须更慢

,因为它不仅仅是普通的函数调用。你告诉

浏览器执行此操作,然后执行此操作与做那个相对而第一个将是* b $ b *总是*慢一点。


You don''t need testing to figure it out, although testing will prove it.
The function pointer is *always* going to be slower. It has to be slower
since it it doing more than a plain function call. You are telling the
browser "Do this, then do that" versus "do that" and the first will
*always* be slower.



我不认为我明白这一点。什么是这个,什么是那个?


Darko

I don''t think I understand that. What''s "this", and what is "that"?

Darko


这篇关于函数指针vs javascript中的直接调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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