2

Possible Duplicate:
Performance difference between jQuery's .live('click', fn) and .click(fn)

Does jQuery live() / delegate() affect significantly performance?

$('a').live('click', ...

$('a').click(... <== is this faster? and how much?

Community
  • 1
  • 1
RB2
  • 23
  • 2

2 Answers2

3

The answer is that it depends:

  • click will be faster for few elements. You'll have one bound function for each element, but no overhead for other clicks.
  • delegate is faster for many elements. There's only one event handler being bound, even for hundreds of elements. The overhead of checking missed clicks is miniscule compared to binding one function for each element.
  • delegate is always faster than live. It can limit where in the page it will listen for events, and does not need to iterate over all the elements when binding.

If you're after hard numbers, I read in an article about delegate that the tipping point is around 3-5 elements. Use click for less than that, and delegate for more. I can't back that up with a link unfortunately, but that's the general rule I've been following.

Magnar
  • 28,550
  • 8
  • 60
  • 65
1

live method is attached to document tree root , and when its triggered, it inspects the target, and if it match, handler function is fired.
click method is binded directly to the node.
There's no simple answer to your question. There are many circumstance. If only thing you need is to bind click event to one element - then use click(). If you have, for example, list of items such as links, and it can be dinamically changed, you should use live(). More info you can find in documentation: live() method

KSDaemon
  • 366
  • 2
  • 9