8

If I'm not returning false from an event callback, or using e.stopPropagation feature of jQuery, the event bubbles up the DOM.

In most scenarios I don't care if the event bubbles or not. Like with this DOM structure example:

​<div id="theDiv">
    <form id="theForm" >
        <input type="submit" value="submit"/> 
    </form>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Normally, I don't have multiple nested submit callback like this:

$('#theDiv').submit(function() {
    alert('DIV!');
});
$('#theForm').submit(function(e) {
    alert('FORM!');
    e.preventDefault();
});​

Fiddle
That DEMO shows the submit event bubbles to a <div>!
It has no difference to me if I stop the Propagation or just prevent default.

In those scenarios, If I stop the propagation will I gain performance benefits?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • It should have no effect whatever, maybe even a hit since the browser bubbles millions of events all the time, it doesn't necessarily bubble them only if there's a listener. – RobG Mar 16 '12 at 02:11
  • @RobG. That's why I want to cancel it... – gdoron Mar 16 '12 at 02:14
  • 1
    Why don't you benchmark it? In theory, stopping propagation = less to do = faster. In practice it depends on the implementation details of the browser and will likely be hardly noticeable either way. – deceze Mar 16 '12 at 02:22

3 Answers3

7

Performance benefits? Yes, there are some slight benefits, as outlined in this performance test between jQuery live() and on(). As @Joseph also noted, the difference between the two is that live propagates all the way up the tree, while on() only goes to the nearest common parent.

In those tests, it is shown that on() can outperform live() by up to 4 times. In practice, that's probably still not worth splitting hairs over, but if you have very deep html structures and lots of event triggers, the performance difference in stopping propagation can be worthwhile, I suppose.

Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139
5

here's a comparison between on() and live() which involve bubbling and the reason jQuery replaced live(). the problem with live() was that events were attached to the document, causing a long travel up the tree to find the handler. what you can do with on() is you can attach the handler to the nearest common parent, thus avoiding the long travel up the tree in search for a handler - which means faster performance.

but i suggest doing your own benchmarks to check.

Joseph
  • 117,725
  • 30
  • 181
  • 234
3

This test shows there is a performance advantage to killing the event early. (15%-30%) And there would probably be a bigger difference on a complex DOM. Its also interesting to note that capturing event listeners seem to be slightly faster than bubbling event listeners regardless of what you do with the event after handling it. Strange but interesting; I only tested in one browser though

Jake
  • 558
  • 4
  • 12