【发布时间】:2012-09-07 17:10:24
【问题描述】:
我正在尝试优化一些遍历和隐藏/显示一些无序列表的基本 jquery 脚本。
这是 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");
谁能解释我做错了什么以及为什么缓存的版本似乎更慢?
【问题讨论】:
-
我大胆猜测,您从缓存版本中获得的好处不会超过缓存数据的开销,因此收集和存储所有数据需要更多的总时间缓存目的而不是检索和返回非缓存版本所需的特定数据。
标签: jquery dom performance