我想優化一些基本的jQuery腳本遍歷和隱藏/顯示一些無序列表。Javascript性能:緩存vs無緩存 - 奇怪的結果
下面是jsperf測試用例:我運行兩個瀏覽器測試http://jsperf.com/caching-vs-no-caching
:Chrome和IE7/IE8和令人驚訝的高速緩存的情況下比較慢 - 通過一點點,但仍。
未優化的版本是:
(function($) {
$.fn.menuManipulation = function() {
this.parents().show();
};
})(jQuery);
$('.menu-vertical li.selected').menuManipulation();
$(".menu-vertical li.selected > ul.static").show();
$('li.static').has('ul').addClass("with-child");
和緩存的一個:
(function($) {
$.fn.menuManipulation = function() {
this.parents().show();
};
})(jQuery);
var menu = $('.menu-vertical');
menu.find('li.selected').menuManipulation();
menu.find('li.selected > ul.static').show();
menu.find('li.static').has('ul').addClass("with-child");
有人能解釋我在做什麼錯?爲什麼緩存的版本似乎是慢?
我會冒險猜測你從緩存版本中獲得的好處不會超過緩存數據的開銷,因此爲了緩存目的而收集和存儲所有數據需要更多的總時間。檢索並返回非緩存版本所需的特定數據。 – taz