开发者

Which has more effective performance?

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

or

$("#search-by-id-wrapper").tog开发者_开发技巧gle();
$("#searchforit").toggle();
$("#findbyid").toggle();
$("#search-block-form .container-inline").toggle();


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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜