views:

986

answers:

3

If I have an element (html) nested in another element and both of them have a click handler attached, clicking the inner element executes its click handler and then bubbles up to the parent and executes its click handler. That's how I understand it.

Do events bubble up the DOM tree if there are no events attached that are the same and if so, is it worth putting a event.stopPropagation() at the end of every handler to stop this and speed things up?

+3  A: 

events almost always bubble up unless event.cancelBubble=true is set or event.stopPropagation() is used. You are only aware of it, though, when one of your event handlers gets tripped.

See http://en.wikipedia.org/wiki/DOM_events for a list of events which bubble. (Note: in the table of HTML events, cancelable refers to the effectiveness of event.preventDefault() or return false to cancel the default action, not bubbling)

Also see http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow, in particular 1.2.1 Basic Flow to understand the capture phase and bubbling phase of event propagation.

EDIT

http://mark-story.com/posts/view/speed-up-javascript-event-handling-with-event-delegation-and-bubbling suggests there is a performance gain by stopping propagation but provides no data.

http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/a9af0aa4216a8046 suggests that browsers should be optimized for bubbling behaviour and says there should be no significant performance difference. Again no data.

http://developer.yahoo.com/performance/rules.html#events provides a good technique for improving event-handling performance, but doesn't directly talk about stopPropagation performance.

Ultimately, you'd have to profile the difference to get a good idea of the benefits on your site.

Jonathan Fingland
Thanks Jonathan. Do you know if there are there any performance gains by stopping propagation all the time?
Vince
not that I'm aware of. Generally bubbling is the expected behaviour, and as such I would suspect browsers would be optimized for that behaviour. I'll see what I can dig up.
Jonathan Fingland
Thanks for your lengthy post Jonathan. I will do some more digging starting at your suggested links. Thanks again.
Vince
glad to help and thanks for the accept.
Jonathan Fingland
A: 

I suppose this behavior is already well optimized by browsers, so you won't be able to catch significant performance boost when stopping propagations (except, perhaps, for really-really complex nested DOM structures). If you are worried by performance and deal with lots of events, you may be interested in event delegation instead.

Also, you should remember your code should stay readable and self-explainable. stopPropagation() is a method used for certain purpose, so using it in every method could be really confusing.

wildcard
Using it in every method is something I wanted to avoid so totally agree there. As for complex nested DOM, unfortunatly I have a rather complex structure no thanks to Component Art controls that nest tables within tables like crazy. I'm experiencing a 2.5 second delay showing 100 records on screen. I timed it from the moment the data was sent to the browser. And its quite possibly due to the way it wires up its events for every single td and tr. Will look into event delegation. Thanks again for your time.
Vince