Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

live('click') and performance

I have a grid and there is a column which contains <a> anchor tag with some additional information in <data-..> tag and has a class name <class='myspeciallink'>. And in my unobtrusive JS script I select all the elements with that class name and apply live('click'). I need that to be live() because the grid gets generated in the runtime.

What happens inside the live('click') handler? I use that additional data and add a <div> to the page based on that data. Which in its turn used to generate jQuery UI dialog. It works great on my computer.

But! How could that work in real-world? Should I be bothered about possible performance implications? I feel that applying live() on more than a dozen elements instantaneously
would affect the performance. Especially with rather complicated handler like mine - it needs to get the data, parse the data, create a div, apply a dialog and etc.

Does that smell like a bad design? Could you suggest a different approach or my concerns are unfounded? Can I use some sort of a profiler tool to find the bottlenecks in my javascript?

UPD: Still nobody suggested any profiling tool. firebug and chrome dev tools are good, but maybe there is something even better?

like image 930
iLemming Avatar asked May 18 '11 15:05

iLemming


People also ask

What is the use of live in jQuery?

jQuery | live() Method This method is used to attach one or more event handlers for the selected elements. It also specifies the function that runs when the event occurs. The event handlers used will work for both current and future elements matching the selector.

WHAT IS ON event in jQuery?

The on() method attaches one or more event handlers for the selected elements and child elements. As of jQuery version 1.7, the on() method is the new replacement for the bind(), live() and delegate() methods.

What does the event data object in jQuery help you do?

The event. data property contains the optional data passed to an event method when the current executing handler is bound.


2 Answers

live("click") is actually better up-front from a performance standpoint: Instead of binding an event handler to each matched element, you're applying a single event handler which waits for events to bubble up and then sees if the element that triggered the event matches the selector .live was called on.

Compare this to $('selector').click(...) which does loop over each element and bind a new event handler. live('click') has no additional overhead regardless of how many page elements match its selector. Depending on how many elements your selector matches, using .live can avoid a delay of up to a few seconds during the initial load of each page.

However, the event handler must check each event which bubbles up against its selector, to see if there is a match. This is going to add a small amount of overhead to every click event, but chances are very good that your users will not notice the difference.

like image 81
meagar Avatar answered Oct 01 '22 06:10

meagar


Peter bailey also has a nice post about this: Performance difference between jQuery's .live('click', fn) and .click(fn)

like image 40
Ali Habibzadeh Avatar answered Oct 01 '22 07:10

Ali Habibzadeh