0

I have a question about the performance of on use as the former live and on use as the former bind.

My question is what is the better choice in terms of performances if we have to create multiple (let's say hundreds) elements in the dom and add an event on each of them :

  • Use 'on' one time as a live
  • Use 'on' each time we create an element as a bind

Thank you for your response.

guts
  • 381
  • 1
  • 9
  • 22

2 Answers2

0

live() is evil. Use on() method if you use jQuery 1.7+. Check first google result for explanation and performance diagrams on() vs live() and understand why live() is bad

mkk
  • 7,583
  • 7
  • 46
  • 62
  • 1
    You don't answer my question (maybe I'm not clear). I want to know if it is better to use 'on' on the document (so like the former 'live') one time or use 'on' directly on the element (so like the former 'bind') but hundreds of times – guts Sep 17 '12 at 08:47
0

I have made some tests on jsperf and it seems that the 'on' used like a 'live' is better than the the 'on' used like a bind on each element. I also add a test with a 'on' used as a 'bind' but at the end of the insertion and directly on the parent off all the elements (and then test in the callback if we click on an element).

http://jsperf.com/jquery-on-as-former-live-vs-on-bind-on-element-vs-on-bi

The test indicate that the as 'live' solution and the as 'bind' on parent à pretty equal in terms of performance.

guts
  • 381
  • 1
  • 9
  • 22