简体   繁体   English

调用 javascript function 的最快方法是什么

[英]What's the fastest way to call a javascript function

Is it faster to call a javascript function defined globally or as a property of a global variable?调用全局定义的 javascript function 或作为全局变量的属性是否更快?

For example:例如:

Option 1选项1

.html:
<input type="checkbox" name="" value="" onClick="dofind()">

.js:
function dofind() { /* code */ }

Option 2选项 2

.html:
<input type="checkbox" name="" value="" onClick="x.dofind()">

.js:    
var x = {
    dofind: function() { /* code */ }
}

Calling a function in a local scope is insignificantly faster.在本地 scope 中调用 function 速度要快得多。 You would need to be making millions of calls before its even noticeable, which isn't the case in an onclick handler.您需要进行数百万次调用才能引起注意,这在onclick处理程序中并非如此。

Neither of those methods are ideal, though.但是,这些方法都不是理想的。 You are better off taking the unobtrusive route by adding an onclick event listener after the DOM has loaded .您最好在 DOM 加载后通过添加onclick事件侦听器来采取不引人注目的路线。

If you consider the scope chain of the event listener, in the first case it must resolve the identifier dofind on the objects on the listener's scope chain.如果考虑事件侦听器的 scope 链,在第一种情况下,它必须解析侦听器 scope 链上对象的标识符dofind Presumably it will not be found on any intermediate object and will be found on the global object.大概不会在任何中间 object 上找到它,而会在全局 object 上找到。 It will then be called.然后它将被调用。

In the second case, the identifier x must be resolved on a similar (identical?) scope chain, and once found, the identifier dofind must be found as a property of that object.在第二种情况下,标识符x必须在类似(相同?)scope 链上解析,并且一旦找到,标识符dofind必须作为该 object 的属性找到。

Logic says that the second method is slower for being longer.逻辑上说第二种方法因为更长而更慢。 However, javascript engines can optimise scope chains and property access if they wish.但是,如果需要,javascript 引擎可以优化 scope 链和属性访问。 It is quite likely (in modern browsers) that subsequent calls to dofind will be made directly, without reference to the scope chain unless the engine believes the chain may have been altered.很有可能(在现代浏览器中)随后对dofind的调用将直接进行,而不参考 scope 链,除非引擎认为该链可能已被更改。

Testing in various browsers will reveal which optimise such calls and which don't.在各种浏览器中进行测试将揭示哪些优化了此类调用,哪些没有。 I think you'll find that in overall application performance, the difference in the call is negligible, particularly as resolving the identifier will occur much faster than a user can initiate a sequence of click events (probably by a factor of 1,000 or more).我想您会发现,在整体应用程序性能方面,调用的差异可以忽略不计,尤其是解析标识符的速度比用户发起一系列点击事件的速度要快得多(可能是 1,000 倍或更多)。

You will not notice any performance difference unless you have thousands of such event handlers loaded on the page.除非您在页面上加载了数千个此类事件处理程序,否则您不会注意到任何性能差异。 When you have thousands of elements loaded on the page with event handlers, the time taken to attach the event handlers is going to be the least of your problem any way.当您在页面上加载了数千个带有事件处理程序的元素时,附加事件处理程序所花费的时间将成为您的问题最少的问题。

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

相关问题 调用JavaScript函数的正确方法是什么? - What's the correct way to call JavaScript Function? 在 JavaScript 中平方一个数字的最快方法是什么? - What's the fastest way to square a number in JavaScript? 在Javascript中迭代对象属性的最快方法是什么? - What's the fastest way to iterate over an object's properties in Javascript? 在另一个文件中调用JavaScript函数的更好方法是什么? - What's a better way to call a JavaScript function in another file? 等待Java中异步函数调用的最有效方法是什么? - What's the most efficient way to wait for an asynchronous function call in Javascript? 在javascript中隐藏和显示表格的所有行的最快方法是什么? - What's the fastest way of hiding and showing all the rows of a table in javascript? 在 JavaScript 中遍历数组的最快方法是什么? - What's the fastest way to loop through an array in JavaScript? 在javascript中保存有序地图的最快方法是什么? - What's the fastest way to keep an ordered map in javascript? 在 JavaScript 中将字符串转换为数字的最快方法是什么? - What's the fastest way to convert String to Number in JavaScript? 将递归 function 的结果连接到 Javascript 中的数组中的最快方法是什么? - What is the fastest way to concatenate results of a recursive function into an array in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM