views:

106

answers:

4

I know it is better coding practice to avoid inline javascript like:

<img id="the_image" onclick="do_this(true);return false;"/>

I am thinking about switching this kind of stuff for bound jquery click events like:

$("#the_image").bind("click",function(){
     do_this(true);
     return false;
});

Will I lose any performance if I bind a ton of click events? I am not worried about the time it takes to initially bind the events, but the response times between clicking and it happening.

I bet if there is a difference, it is negligible, but I will have a ton of functions bound. I'm wondering if browsers treat the onclick attribute the same way as a bound event.

Thanks

+1  A: 

In my work, it depended. I moved all of my events to jquery. Then I profiled the javascript using FireBug to see what was taking the longest. Then I optimized those taking the longest.

If its just a few, you won't notice any degradation. If its hundreds or thousands, then you might.

Eric Cope
+1  A: 

Save yourself the worry, use the live event

$("#the_image").live("click",function(){
     do_this(true);
     return false;
});

One event, with no performance hit with multiple items.

Mike Robinson
"no performance hit with multiple items" is definitely false. You might not notice it on a couple elements, but put a live event on hundreds or thousands of elements and you'll see a performance hit. Also, I don't see why you'd need a live event on a single element. (Your example used an ID for the CSS selector, which is unique.)
musicfreak
I take back my previous comment; apparently the jQuery team optimized their `live` algorithm with jQuery 1.4, so the performance hit that comes with using `live` is virtually gone. +1
musicfreak
A: 

The difference is negligible. If you have to bind to many items in the page, there can be a performance hit, and you may want to bind to a higher level object, and simply intercept the target item (image) where you are binding the click to a containing DIV tag. Other than that, it should be fine and will depend on your use case specifically.

Look into event bubbling in javascript for more specifics.

Tracker1
Thanks, now I'm wondering does the performance hit come because it is jquery, or does it hurt performance equally if I do:the_image.onclick = function(){do_this(true);};Thanks again!
Geromey
It's mainly the sheer number of event listeners, especially in IE, because in IE the js engine and the rendering/DOM engine are cross COM boundaries. The difference between the two is negligable, but using .live can help, so can binding to something higher in the DOM so you can catch it on an event bubble.
Tracker1
A: 

You might notice an initial performance hit as the events are bound to their respective elements (depending on how many elements you're doing this for -- if it's only a couple, probably not), but I doubt you'll notice a difference in performance in the actual execution of the events, because after the initial binding, the two methods are essentially equal.

I personally think the jQuery way is much more readable and maintainable, so go with that, and if you notice a performance hit, optimize it. Profile it before you make any premature optimizations, though.

musicfreak