Why it is bad practice to register events to document in jQuery?
<p>Whenever I read about event registration in jQuery they all say that we should try to add event handlers to the nearest parent, and avoid adding event listeners to the document, because according to resources they are slow and not efficient.</p>
<p>But why they are slow? Apparently it's because the event will have to bubble up to the document, which will take time. Then it will be compared to a list of selectors to call, e.g.</p>
<pre><code>$(document).on(&quot;click&quot;, &quot;.abc&quot;, function(){  })
$(document).on(&quot;click&quot;, &quot;.abc2&quot;, function(){ })
</code></pre>
<p>So here if I click an element, the click event will be bubble up to the document and then it will match the selector list (&quot;.abc, .abc2&quot;)...and that is inefficient. OK I got it but what if I have only have one selector in the list ? e.g.</p>
<pre><code>$(document).on(&quot;click&quot;, &quot;*&quot;, function(){  }) 
</code></pre>
<p>Will it be slow too ? If so why?</p>
<p>Basically i'm trying to create a google's jsaction similer lib, so I will write like this:-</p>
<pre><code>$(document).on(&quot;click&quot;, &quot;[jsaction]&quot;, function(){  })
</code></pre>
<p>Because this will be the only selector, so i don't think it will be slow ? or will it be ?</p>
<p>and if attaching event to document is not efficient, then what about a completely ajax application? my application is completely ajax, and every page will be downloaded by ajax. Is there another, more efficient solution?</p>