Question

Lets say you have 100,000 divs inside a parent div. A jQuery plugin is initialised whenever any of these divs are clicked. If you assign a single class to all these divs and use that as the selector, would it slow performance dramatically? Is there a better way of doing this so that you localise the plugin to each individual div in some way?

Était-ce utile?

La solution 2

Binding events to 100,000 elements would be the performance hit, not the class (assuming the class is there by default, not added by javascript.)

event delegation would be appropriate here.

$(someParent).on("click","div.someclass",function(){
    $(this).somePlugin();
});

Autres conseils

Classes should not slow performance dramatically. They only overhead is the time it takes to read over the fact that it has a class which is practically nothing. Actions that actually take processing power only occur when changes are made to the div. This is going to happen regardless of how you set up the click listener.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top