简体   繁体   中英

Triggering Non-existent jQuery Events and Performance

I have some javascript that registers and triggers custom events in jQuery - what I'm wondering is what kind of performance hit, if any, is taken by triggering an event that has no event handlers bound to it - example:

var someData = ['blah', 'foo', 'bar'];
$('#somediv').trigger('StateChange', [someData]);

In the above (very short) example, there is no $('#someDiv').on('StateChange', ...) event handler, so is it a waste to trigger the event and would I see a significant performance hit if, say, I had 5-10 events trigger on a given element with no handlers bound?

No, for 5-10 objects you won't see a significant performance hit for triggering an event that has no handler.

Keep in mind that the point of an event system is that you don't need to worry about whether or not the object in question has been assigned a handler for the requested behavior. This way you can casually trigger the event without needing any sort of testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM