Skip to content

Instantly share code, notes, and snippets.

@GavinJoyce
Last active February 15, 2020 08:12
Show Gist options
  • Save GavinJoyce/4004b6ad7dda427e629f to your computer and use it in GitHub Desktop.
Save GavinJoyce/4004b6ad7dda427e629f to your computer and use it in GitHub Desktop.
jQuery event leak detection
var walk_the_DOM = function walk(node, func) {
func(node);
node = node.firstChild;
while (node) {
walk(node, func);
node = node.nextSibling;
}
};
var totalSubscriptionCount = 0;
walk_the_DOM(document.body, function(node) {
var events = jQuery._data( node, "events" );
if(events) {
// console.log('node', node.id, node.nodeName);
// console.log('events', events);
Object.keys(events).forEach(function(key) {
var subscriptionCount = events[key].length;
totalSubscriptionCount += subscriptionCount;
if(subscriptionCount > 3) {
console.log('POSSIBLE MEMORY LEAK --> ', node.nodeName, node.id, key, events[key].length, events, node.className);
} else {
console.log(' -->', node.nodeName, node.id, key, events[key].length, node.className);
}
});
}
});
console.log('totalSubscriptionCount: ', totalSubscriptionCount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment