3

I'm trying to find the most productive way to hanging event.

Lets imagine we have this structure:

<ul>
    <li> 1st li </li>
    ...
    <li> 99999th li </li>
<ul>

So, we have about 10000 li elements and we want to add event listener to all this elements, lets say we can use jQuery too.

First thought

<li onclick="console.log(this)" >...</li>

bad part of this solution - we have increase a size of html markup because always repeating onlick.

Second thought

$('ul li').on('click', function(){ 
    console.log(this)
})

Of course this solution is not an option because we add 10000 event listeners, obviously not good for performance.

Third thought, event delegation

$('ul').on('click', 'li', function(){
    console.log(this)
})

looks really good as for me, we use only one event listener, not trashed html markup and it works as we need.

So, I asking this question, because sometimes I look at the source of different sites and big part of them use first way. Why? First way has some advantages?

I guess that maybe because it is easier to catch a dynamically added elements to the page, but may be there is something I do not know?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Denis
  • 2,429
  • 5
  • 33
  • 61
  • What about 1 event listener on
      ?
    – Brian Jun 22 '13 at 21:07
  • I think the best way might be to use as your base tag for the .on statement - that way you have only one watcher, and not a bunch for every ul. What do you think? – Christian Stewart Jun 24 '13 at 01:40
  • sure, if you have only one
      on the page :) what about a lot of different ul's for the various purposes on the same page, where you have to add own listeners to each of them?
    – Denis Jun 24 '13 at 20:11

2 Answers2

5

I wouldn't look at "big sites" for the best web development practices. The first way has really no advantages over the other two, as it mixes JavaScript with HTML and is harder to maintain.

Event delegation will be lighter than the other two and will have the advantage of being able to handle dynamically generated elements, so in this case, it'd probably be the best choice. I would profile your application with both approaches before choosing.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • +1 except you should be aware of the pitfalls of delegation... if an element in your document intercepts the "bubbling event" & returns false or calls event.stopPropagation(); it will never reach your delegated event handler. This should be easy to spot when an event doesn't trigger as long as you remember this. – Precastic Jun 22 '13 at 22:08
5

you can find your answer here- jQuery.click() vs onClick ,
As you can see http://jsperf.com/testper test, event delegation approach is the best approach for event hanging.In your case where you have 10000 or more events than surely 3rd approach is best for you.

Community
  • 1
  • 1
Anshul
  • 9,312
  • 11
  • 57
  • 74
  • I agree that the 3rd approach is the best. But your test is poor because it tests only event binding and not triggering. – claustrofob Jun 22 '13 at 22:14
  • @claustrofob I was trying to be specific to question, next time I will take care, thanks. – Anshul Jun 22 '13 at 22:17