3

I really like using .live() in jQuery for click() focus() blur() and other interaction events.

I do a lot of prototyping, so I find it gives me great flexibility if I want to dynamically add elements. For that reason, I find myself drawn to the idea of using it by default all the time. Is this a good idea, or is this bad performance?

Does using .live('click',function(){}) slow things down in a way that .click(function(){}) doesn't?

daveyfaherty
  • 4,585
  • 2
  • 27
  • 42
  • if you are not appending elements dynamically to the DOM or you dont want the click handler to be attached to dynamically added elements dont use `live` because it costs you in terms of performance. – Rafay Sep 27 '11 at 10:08
  • You might probably go with event bubbling sometimes instead. – julx Sep 27 '11 at 10:08
  • 1
    Delegate() is very similar to live() except that instead of bubbling all the way to the body, you can specify a parent container where the bubbling will stop. It's more performant and the syntax is very similar. http://api.jquery.com/delegate – Malevolence Sep 27 '11 at 10:14
  • 1
    check this might help you : http://stackoverflow.com/questions/2690370/live-vs-bind – Pranay Rana Sep 27 '11 at 10:15

4 Answers4

1

I think following answer will be suitable for the performance impact it does create How does jQuery .live() work?

Community
  • 1
  • 1
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
1

Regarding the performance, using the live is better in most cases. However live has several pitfalls, which are described in the documentation here http://api.jquery.com/live/#caveats.

bjornd
  • 22,397
  • 4
  • 57
  • 73
1

I remember that .live uses event bubbling.

In my experience, I've seen noticeable performance hit using .live in big document with a frequently triggered event like mouseover.

jQuery Doc:

But as of jQuery 1.4, event bubbling can optionally stop at a DOM element "context".

So, you can use that to minimize the performance effect.

Dave
  • 12,117
  • 10
  • 46
  • 52
1

If you don't need your events to bubble up to the top of the DOM and you know the context in which your event will occur then delegate() is a much better choice in terms of performance. See this stackoverflow post on why delegate() is better than live() in this regards.

Community
  • 1
  • 1
amurra
  • 15,221
  • 4
  • 70
  • 87