Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

livequery performance

I've recently discovered that livequery plugin for jQuery may be quite wasteful, as it does not use event delegation but binds all bindable events and re-checks the whole DOM on each change

if anyone has more information or suggestions on best practices using livequery and .live(), I would be very grateful

like image 344
Zathrus Writer Avatar asked Jan 27 '11 15:01

Zathrus Writer


1 Answers

It is rare that you would actually need a plugin like livequery. Probably the only time you really need it is if you need to react to changes to the DOM made by some other jQuery code that you can not modify.

While .live() does use event delegation, it does it on the document level, which means that it needs to process all events on the page to see if they match the selectors provided per event type.

A better alternative (IMO) to both of those is the delegate()(docs) method which uses event delegation just like .live(), but lets you constrain it to a specific portion of the page.

$('#someContainer').delegate('a.someButton', 'click', function() {
    // do something when an "a.someButton" inside "#someContainer" is clicked
});

Note that event delegation methods respond to browser events, not to changes to the DOM. If you need to run some code based on a change to the DOM you've made, you need to run that code when you make that alteration to the DOM.

like image 67
user113716 Avatar answered Sep 25 '22 14:09

user113716