WestieUK
WestieUK

Reputation: 224

Which has more effective performance?

$("#search-by-id-wrapper, #searchforit, #findbyid, #search-block-form .container-inline").toggle();

or

$("#search-by-id-wrapper").toggle();
$("#searchforit").toggle();
$("#findbyid").toggle();
$("#search-block-form .container-inline").toggle();

Upvotes: 0

Views: 93

Answers (2)

AKX
AKX

Reputation: 169388

With a cursory look at Sizzle, jQuery's selector library, it chunks the selector string using commas anyway...

My intuition thus says it will be faster to use the first form, and even better so if you cache the selected jQuery object.

var $searchThings = null;
/* ..... */
function toggleSearchThings() {
     $searchThings = $searchThings || $("#search-by-id-wrapper, #searchforit, #findbyid, #search-block-form .container-inline");
     $searchThings.toggle();
}

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318718

Even if there is a slight performance difference which I doubt since the string is most likely split and multiple getElementById calls are used in both cases (however, the latter case creates 4 instead of 1 jQuery wrapper objects) it's most likely neglectible.

So you should use the latter since it's much more readable.

Upvotes: 0

Related Questions