开发者

Can an empty event slow down Javascript processing?

I have an empty $(window).resize function

I was wondering, would commenting it out have any effect

on the processing of the page (in sp开发者_JS百科eed)?


I just did some profiling and yes, calling an empty event function does cause a performance hit, though a very mild one. Tests done in Chrome.

1ms - Trigger click without any event functions bound

1ms - Trigger click with 1 empty event function bound

1ms - Trigger click with 10 empty event functions bound

2ms - Trigger click with 100 empty event functions bound

11ms - Trigger click with 1000 empty event functions bound


Well, if the method doesn't do anything, why keep it around?

While the speed increase would be negligible it would still make sense to remove the callback since you're method would still get triggered every time the page is resized. So yes, go ahead and remove it.


Measurably: probably not.

Commenting out the handler results in the function not being bound to the resize event, thus resulting in a lower memory footprint; the code still has to be downloaded to the client, so bandwidth-wise, you would gain nothing (as a matter of fact, commented code results in more data being downloaded to the client).

The main question, however, is: if you have an empty function in your code, why would you keep it there?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜