Frage

Does binding an event on document have any performance concerns?

i.e:

$(document).on('mouseenter mouseleave', function(e){
    if (e.type === 'mouseenter'){
        $(this).find( //some element and do something...

Vs

$(".myElement").hover(function(){
    $(this).find( //some element and do something...

I think keeping a track via document will use more processing power then assigning the event only a limited DOM elements?

War es hilfreich?

Lösung

You might need to implement a performance test to be sure about the actual difference, but I guess it should be a minimal impact, because binding events on the document will mean that you catch any element event once it bubbles to the top-most element in the document.

Anyways, skipping the "performance argument", your case looks better when you bind a handler on the nearest parent. It's not only about performance: it's more logical.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top