[thelist] Javascript: Closures vs Event Delegation

Christian Heilmann codepo8 at gmail.com
Tue Feb 10 19:10:08 CST 2009

Jay Turley wrote:
> Hi, all-
> I'm working on an AJAXy application with much javascript goodness involved.
> Currently, as I iterate over a list of items retrieved from the
> database, then insert them into the DOM, I am attaching click handlers
> to the displayed elements as closures which contain the item IDs from
> the database in their execution contexts.
> However, as the list of items grows (and it will continue to do so), I
> am wondering if I should move to event delegation for processing the
> clicks to avoid having umpty-hundreds of closures floating around at
> any given time. This requires unique ids for each element which can be
> plucked out for use by the event handlers (think the new live()
> functionality from jQuery 1.3).
> I'm wondering if anyone has looked into the performance issues of
> closures versus event delegation (obviously the event delegation is
> going to use less memory), and if so what tips or comments you might
> have.
> Thanks a ton!
> -Jay Turley
Event Delegation is the way to go. If you do any DOM access in your 
closures IE has a lot of memory leaks. It is also much easier for you to 
deal with the event handling as you can listen to any element.

More information about the thelist mailing list