views:

104

answers:

3

Hello All - Out of curiousty which of the following code is more efficient (if neither, what would be the best way?)

Backstory - Building a small image carousel and the code in question has to do with the controls (prev, pause/play, next)

<ul class="controls"> 
    <li> <a href="#" class="prev">  Previous Image </a> </li>
    <li> <a href="#" class="pause"> Pause          </a> </li>
    <li> <a href="#" class="next">  Next Image     </a> </li>
</ul>

// inside document.ready()
$(".controls a").click(function() {
    var cur_class = $(this).attr("class");

    if (cur_class == "pause") {
        // do something
    } else if (cur_class == "prev") {
        // do something
    } else if (cur_class == "next") {
        // do something
    }
)};

// OR THIS
$(".controls a.prev").click(function() { /* do something */ });
$(".controls a.pause").click(function() { /* do something */ });
$(".controls a.next").click(function() { /* do something */ });

Thanks M.

+2  A: 

I think the following is the fastest way. It only fetches a DOM nodelist once and filters it instead of fetching it three times

$('.controls a')
.filter('.prev')
.click(function(){/*...*/})
.end()
.filter('.pause')
.click(function(){/*...*/})
.end()
.filter('.next')
.click(function(){/*...*/});
Jacob Relkin
Is that actually faster than just doing $(".controls a.prev") ?
Matt
I think it is. It only fetches a DOM nodelist once and filters it instead of fetching it three times.
Jacob Relkin
I think .delegate() was made for this specific problem so it's probably more efficient than .filter()
Jonathan
+3  A: 

Almost no difference. As your building a carousel the big bottleneck will be the images you load.

I understand that, but was always curious about which technique using jQuery is the more (most?) efficient. I am not having any bottlenecks or load issues or anything like that, just a question of efficiency, especially for future jQuery use.
Matt
when will people learn "most efficient" doesn't mean anything. "most efficient" what, space, time, donuts, monkeys? It is javascript, if you mean "efficient" as in fastest, then do the work, profile if your self and don't rely on people that "think" something is one thing or another, or post untested code. Then you have to test all the javascript interpreters in all the browsers to find out that what you "think" you discovered isn't applicable across environments.
fuzzy lollipop
@fuzzy lollipop, chillax bro. Sometimes people consult others, cause you know, other people may have wisdom to impart. Learning from others != laziness.
Mark
not putting some timing statements around 2 blocks of code that are already written == lazy. And this is premature micro optimization at its worst!
fuzzy lollipop
+5  A: 

Best option is to use .delegate(). Its a new event added to jQuery 1.4.2 and its much better than just using click.

.click() adds a new event for every anchor tag.

.delegate() 'waits' for a click (or any event specified) to be made before adding a new event (for the specific clicked element only).

$(".controls").delegate("a", "click", function() {

    var cur_class = $(this).attr("class");

    if (cur_class == "pause") {
        // do something
    } else if (cur_class == "prev") {
        // do something
    } else if (cur_class == "next") {
        // do something
    }

}

Note: the code is not tested, read the .delegate() info in the jQuery documentation for more info.

Maybe you need to add an id to the <ul>:

(<ul class="controls" id="ul_id">) and then use $("#ul_id").delegate("a", "click", function() { / ... }.

Hope that helps.

Jonathan
posting untested code, boo
fuzzy lollipop
It's basic jQuery man...
Jonathan