Last active
February 15, 2020 08:12
-
-
Save GavinJoyce/4004b6ad7dda427e629f to your computer and use it in GitHub Desktop.
jQuery event leak detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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