Skip to content

Instantly share code, notes, and snippets.

@karolk
Created April 19, 2011 11:17
Show Gist options
  • Save karolk/927151 to your computer and use it in GitHub Desktop.
Save karolk/927151 to your computer and use it in GitHub Desktop.
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.')
@karolk
Copy link
Author

karolk commented Apr 19, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment