1

Are there any disadvantages of using jQuery's 'live' consistently throughout my application?

If i attach click handlers to all my elements that i want and then decide to make my application AJAX, i'd have to go through and add a load of 'live' handlers in my jQuery.

I'm talking performance and HTTP requests etc...what are the disadvantages?

benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • Possible duplicate of http://stackoverflow.com/questions/1368223/performance-difference-between-jquerys-liveclick-fn-and-clickfn – Alex Turpin Sep 15 '11 at 15:05
  • Using a lot of `live()` must indeed slow down the application, because every element introduced to the DOM (or moved around in it) must be checked whether it matches a `live()` rule... Interested to see what comes up – Pekka Sep 15 '11 at 15:06
  • The performance would all be on the client-side, I don't think it would be very noticeable. Here is a little info about caveats of `.live()` http://api.jquery.com/live/#caveats – Rusty Jeans Sep 15 '11 at 15:07

2 Answers2

8

You should never use live. It was a mistake on jQuery's part when they introduced it. Only use delegate.

When using live, jQuery will have to listen to every single click on the document. In most cases, this is overkill. Imagine this:

<table id="products">
    <tr>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
    </tr>
</table>

Now, you want to bind a click event handler on all of those rows. If you go the live way, you'll do this:

$('tr').live('click', function(){...});

What's happening here is that whenever any element on the whole entire page is clicked, jQuery will have to run it through your selector (in this case tr) to see if it matches. That is wasting a lot of resources for no good reason. You can accomplish the same thing by:

$('#products').delegate('tr', 'click', function(){...});

which will cause jQuery to only listen on click events inside that table (although it'll still run that selector check on every type of element clicked).

Now, sometimes you really do need to listen for clicks on the entire page. But, even if you do need that, you're still better off using $(document).delegate('a', 'click', function(){...});.

Why?

Imagine the following scenario:

$('a').live('click', function(){...});

What you're doing here is traversing the DOM for all the a tags, but you're not doing anything with that collection. You're merely doing what you could've accomplished with:

$(document).delegate('a', 'click', function(){...});

but without traversing the DOM first!


Further reading:

http://api.jquery.com/live/#caveats
http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/


Update: With the advent of jQuery 1.7, the new .on method was introduced:

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.

So, in our examples above, you'd use this:

$('#products').on('click', 'tr',  function(){...});

and this

$(document).on('click', 'a', function(){...});

respectively.

Although the old methods would still be functional, this is now the preferred way.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 2
    -1, this doesn't really explain why or provide any references – cobbal Sep 15 '11 at 15:07
  • 1
    Indeed, please provide some benchmarks, tests or references to support this. – Alex Turpin Sep 15 '11 at 15:08
  • 1
    Just go read the doc for delegate ;) He's right, delegate is a more efficient than live. One of the big problem of live events, is that you cant stop their propagation. And here is a link about the perfs: http://jsperf.com/jquery-live-vs-jquery-delegate/15 – Robin Sep 15 '11 at 15:10
  • OK, I'm satisfied. Thanks for the explanation. (I edited it to get around a bug where I couldn't undo the downvote) – cobbal Sep 15 '11 at 15:17
1

I can think of two disadvantages:

  1. The list of caveats listed in the docs: http://api.jquery.com/live/#caveats
  2. For an inexperienced developer, using .live masks the event delegation that is occurring, and so it may not be clear how this is working, or may impede understanding event delegation

In general, though, it's a very convenient method. I'd encourage taking the time to understand event delegation, but also to use .live whenever it makes sense.

Philip Schweiger
  • 2,714
  • 1
  • 18
  • 27
  • Indeed. `.live()` is one of those things that has a purpose, but is probably abused far more than it should be. There's definitely use-cases for it, but follow the general rule: use something else unless you can't. ;) – Chris Pratt Sep 15 '11 at 15:12