Asaf
Asaf

Reputation: 8216

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 speed)?

Upvotes: 2

Views: 296

Answers (3)

mVChr
mVChr

Reputation: 50205

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

Upvotes: 2

Aron Rotteveel
Aron Rotteveel

Reputation: 83203

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?

Upvotes: 0

JohnP
JohnP

Reputation: 50039

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.

Upvotes: 0

Related Questions