在 javascript 中,定义内联函数与将其作为引用传递有何权衡?

发布于 2024-11-25 02:47:51 字数 1387 浏览 0 评论 0原文

因此,假设我有一大组元素想要附加事件侦听器。例如,我希望单击时每行都变成红色的表格。

所以我的问题是哪个最快,哪个使用最少的内存。我知道这(通常)是一种权衡,所以我想知道每个方案的最佳选择。

使用表示例,假设有一个所有行元素的列表,“rowList”:

选项 1:

for(var r in rowList){
     rowList[r].onclick = function(){ this.style.backgroundColor = "red" };
}

我的直觉是这是最快的,因为少了一个指针调用,但是大多数内存密集型,因为每个行列表都有自己的函数副本,如果 onclick 函数很大,这可能会变得严重。

选项 2:

function turnRed(){
     this.style.backgroundColor = "red";
}
for(var r in rowList){
     rowList[r].onclick = turnRed;
}

我猜这只会比上面的慢一点(哦不,多一个指针取消引用!),但内存密集程度要低得多,因为浏览器只需要跟踪该函数的一份副本。

选项 3:

var turnRed = function(){
     this.style.backgroundColor = "red";
}
for(var r in rowList){
     rowList[r].onclick = turnRed;
}

我认为这与选项 2 相同,但我只是想把它扔掉。对于那些想知道此选项与选项 2 之间有何区别的人:定义函数的 JavaScript 差异

奖励部分:Jquery

同样的问题:与

$('tr').click(function(){this.style.backgroundColor = "red"});

function turnRed(){this.style.backgroundColor = "red"};
$('tr').click(turnRed);

和:

var turnRed = function(){this.style.backgroundColor = "red"};
$('tr').click(turnRed);

So, let's say I have a large set of elements to which I want to attach event listeners. E.g. a table where I want each row to turn red when clicked.

So my question is which of these is the fastest, and which uses the least memory. I understand that it's (usually) a tradeoff, so I would like to know my best options for each.

Using the table example, let's say there's a list of all the row elements, "rowList":

Option 1:

for(var r in rowList){
     rowList[r].onclick = function(){ this.style.backgroundColor = "red" };
}

My gut feeling is that this is the fastest, since there is one less pointer call, but the most memory intensive, since each rowlist will have its own copy of the function, which might get serious if the onclick function is large.

Option 2:

function turnRed(){
     this.style.backgroundColor = "red";
}
for(var r in rowList){
     rowList[r].onclick = turnRed;
}

I'm guessing this is going to be only a teensy bit slower than the one above (oh no, one more pointer dereference!) but a lot less memory intensive, since the browser only needs to keep track of one copy of the function.

Option 3:

var turnRed = function(){
     this.style.backgroundColor = "red";
}
for(var r in rowList){
     rowList[r].onclick = turnRed;
}

I assume this is the same as option 2, but I just wanted to throw it out there. For those wondering what the difference between this and option 2 is: JavaScript differences defining a function

Bonus Section: Jquery

Same question with:

$('tr').click(function(){this.style.backgroundColor = "red"});

Versus:

function turnRed(){this.style.backgroundColor = "red"};
$('tr').click(turnRed);

And:

var turnRed = function(){this.style.backgroundColor = "red"};
$('tr').click(turnRed);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月光色 2024-12-02 02:47:51

这是您的答案:

http://jsperf.com/function-assignment

选项 2 速度更快,使用更少记忆。原因是选项 1 为循环的每次迭代创建一个新的函数对象。

Here's your answer:

http://jsperf.com/function-assignment

Option 2 is way faster and uses less memory. The reason is that Option 1 creates a new function object for every iteration of the loop.

入怼 2024-12-02 02:47:51

就内存使用而言,您的选项 1 正在为数组中的每一行创建一个不同的函数闭包。因此,此方法将比选项 2 和选项 3 使用更多的内存,后者仅创建一个函数,然后传递对其的引用。

出于同样的原因,我也希望选项 1 是三者中最慢的。当然,实际性能和内存使用方面的差异可能非常小,但如果您想要最高效的方法,请选择选项 2选项 3 (它们几乎相同,两者之间唯一真正的区别是 turnRed 可见的范围)。

对于 jQuery,所有三个选项都将具有相同的内存使用和性能特征。在每种情况下,您都会创建并向 jQuery 传递单个函数引用,无论您是否内联定义它。

您的问题中没有提到的一个重要注意事项是,使用大量内联函数可能会很快将您的代码变成一团难以阅读的混乱,并使其更难以维护。这并不是什么大问题,因为您的函数中只有一行代码,但作为一般规则,如果您的函数包含超过 2-3 行,则最好避免内联定义它。相反,将其定义为选项 2选项 3,然后传递对其的引用。

In terms of memory usage, your Option 1 is creating a distinct function closure for each row in your array. This approach will therefore use more memory than Option 2 and Option 3, which only create a single function and then pass around a reference to it.

For this same reason I would also expect Option 1 to be the slowest of the three. Of course, the difference in terms of real-world performance and memory usage will probably be quite small, but if you want the most efficient one then pick either Option 2 or Option 3 (they are both pretty much the same, the only real difference between the two is the scope at which turnRed is visible).

As for jQuery, all three options will have the same memory usage and performance characteristics. In every case you are creating and passing a single function reference to jQuery, whether you define it inline or not.

And one important note that is not brought up in your question is that using lots of inline functions can quickly turn your code into an unreadable mess and make it more difficult to maintain. It's not a big deal here since you only have a single line of code in your function, but as a general rule if your function contains more than 2-3 lines it is a good idea to avoid defining it inline. Instead define it as in Option 2 or Option 3 and then pass around a reference to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文