Question: What is a particular performance concern when dealing with event handlers, and how can you cope with it?
- Finding which element an event occurred on is expensive. Assign most events to document.body and use .is() to act on the element of interest.
- Some events, such as mousemove and scroll, happen a lot on a typical page. Debounce or throttle their handlers to make sure the handlers are not called more than you actually need.
- Listening for an event that does not exist can create serious memory leaks. Be careful to spell event names correctly to avoid consuming too much memory.
- DOM elements with an ID wil fire events more efficiently than with classes. Always use IDs instead of classes where possible.
Answer: The correct answer of the above question is Option B:Some events, such as mousemove and scroll, happen a lot on a typical page. Debounce or throttle their handlers to make sure the handlers are not called more than you actually need.