tags:

views:

31

answers:

2

I am loading HTML page through ajax and then doing a bunch of searches using selectors:

$.ajax({
    ...
    dataType: "html",
    success: function(html) {
        $("#id1", html);
        $(".class", html);
        //...
    }
}

Should I extract $(html) into a variable and use it as a content, or it doesn't matter (from performance point)?

success: function(html) {
        $html = $(html);
        $("#id1", $html);
        $(".class", $html);
        //...
    }
A: 

I would do this:

success: function(html) {
            $(html)
                .find("#id1").do().do().end()
                .find(".class").do().end();
}
Mouhannad
I will have hundreds of lines and all searches are not in the same place.
serg
A: 

You should always minimize number of $() calls, as they are expensive. Every one of these calls constructs new JQuery object, so saving these objects to variables is a good thing to do.

atomizer