In my current day to day work I spend a lot of time writing Javascript, and a significant portion of that Javascript involves Events. Of course creating event observers is dead simple with Prototype, as is stopping event observers. For better or for worse Javascript isn’t C, and sometimes we lose track of which events we’re observing, and which we’ve cleared, and we start to create a fairly large Event.cache.
Stumbling across Juriy Zaytsev’s Event Counter Bookmarklet I decided to do a bit of debugging of an internal data analyst application current under development, and to my astonishment discovered that after a few minutes within the application I had nearly 4,000 Event observers hanging around. Granted, this application handles editing of rather complex documents, but something wasn’t getting cleaned out properly. After manually digging out the low hanging fruit, I had managed to drop half of those dangling observers, but I was still missing quite a few, and over time these smaller leaks were bound to bog down the application. I decided I needed a bookmarklet with a bit finer grain, that could enumerate the types of observers in the document. Building upon Juriy’s use of the inject method of Prototype Enumerables I built the following simple script:
javascript:alert(
"Event Observersn" + $H(Event.cache).inject( $H(),
function (acc, p) {
$H(p.value).each(
function (evtT) {
if ( !acc.get(evtT.key) )
acc.set(evtT.key, 0);
acc.set(evtT.key,
acc.get(evtT.key) + evtT.value.size()
);
}
);
return acc;
}
).inject( "",
function (acc,p) {
acc += "n"
+ p.key
+ ":"
+ ( " ".times(40 - p.key.length) )
+ p.value;
return acc;
}
)
)
Which you can grab with this bookmarklet.
This bookmarklet produces a popup with an enumerated list of observers by type:
![]()