Created
April 19, 2011 11:17
-
-
Save karolk/927151 to your computer and use it in GitHub Desktop.
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 nodes = 0, | |
unique_handlers = []; | |
jQuery && jQuery('*').each(function() { | |
var ev = jQuery(this).data('events') | |
if (ev) { | |
for (var e in ev) { | |
eh_arr = ev[e]; | |
nodes+=eh_arr.length; | |
for (var j=0, l=eh_arr.length; j<l; j++) { | |
eh = eh_arr[j].handler, sb = false; | |
for (var k=0, m=unique_handlers.length;k<m; k++) { | |
if (unique_handlers[k] === eh) { | |
sb = true; | |
break; | |
} | |
} | |
sb || unique_handlers.push(eh); | |
} | |
} | |
} | |
}) | |
console.info('There are ' + unique_handlers.length + ' unique event handlers bound to', nodes, 'nodes.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Counts all event handlers attached to DOM nodes with jQuery bind (requires jQuery 1.4), while checking them for uniqueness. For performance (esp. in web applications) you should have smallest number of event handlers attached to fewest amount of nodes. Consider using named functions instead of anonymous functions and event delegation to reduce both numbers.