Skip to content

Instantly share code, notes, and snippets.

@LukeChannings
Created March 15, 2013 12:13
Show Gist options
  • Save LukeChannings/5169471 to your computer and use it in GitHub Desktop.
Save LukeChannings/5169471 to your computer and use it in GitHub Desktop.
Counts the number of listeners attached to a pubsub listener tree.
function countListeners(tree) {
var count = 0
for (var i in tree) {
if (tree.hasOwnProperty(i) && typeof tree[i] === "object") {
count += countListeners(tree[i])
}
if (typeof tree._listeners === "function") {
count += 1
} else if ( tree._listeners instanceof Array ) {
count += tree._listeners.length
}
}
return count
}
@LukeChannings
Copy link
Author

Usage:

countListeners(pubsubInstance.listenerTree)

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