There's a bunch of questions about how to remove all "specific" handlers (e.g. onclick) from elements, or purge all events from an element, etc.
- How to remove all Click event handlers in Jquery
- Jquery: how to release all event handlers, dom changes?
This question is a bit broader. We were facing huge memory leaks in the asp.net site under IE7 and IE8, and one of the tweaks to shrink those leaks was to unbind all events from all elements on the page. Basically this:
$('*').off();
Now, we're trying to improve the performance of the application and, to no surprise, this particular line is one of the hot spots.
So, the question is: are there any way to achieve the same result (explicitly removing all the handlers from all the elements in the document) more efficiently?
No-go options:
- "Reload the page" - memory leaks are preserved this way.
- "Every time you attach a handler you store it in an array" - there are way too much such places
What I was thinking of is to use some jQuery internals (maybe even non-public API) to leverage the detection of element with handlers attached. But it's slightly above my current competency with jQuery. If it helps, we're targeting jQuery 1.7.1.