简体   繁体   English

Javascript性能:缓存与不缓存-奇怪的结果

[英]Javascript performance: Caching vs No Caching - weird results

I am trying to optimize some basic jquery script that traverses and hides/shows some unordered lists. 我正在尝试优化一些遍历和隐藏/显示一些无序列表的基本jquery脚本。

Here's the testcase in jsperf: http://jsperf.com/caching-vs-no-caching 这是jsperf中的测试用例: http ://jsperf.com/caching-vs-no-caching

I run the test in two browsers: Chrome and IE7/IE8 and surprisingly the cached case is slower - by a little bit but still. 我在两种浏览器中运行了该测试:Chrome和IE7 / IE8,令人惊讶的是,缓存的情况更慢-有点,但仍然如此。

The unoptimized version is: 未优化的版本是:

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

and the cached one: 和缓存的一个:

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

Could someone explain what am I doing wrong and why does the cached version seem to be slower? 有人可以解释我在做什么错吗,为什么缓存的版本似乎慢一些?

Short answer: Selectors are actually pretty fast, but find is slow as hell. 简短的答案:选择器实际上非常快,但是find却慢得要命。 Your cached version has introduced multiple find calls - that is what is slow. 您的缓存版本引入了多个find调用-这很慢。

Slightly longer answer: You only really get benefir from caching the jQuery collection if you keep re-using it as-is. 稍长的答案:如果您继续按原样重复使用,则只有从缓存jQuery集合中真正受益。 Take a look at this test case where the cached version clearly runs faster: http://jsperf.com/cachingjq 看一下这个测试案例,其中缓存的版本显然运行得更快: http : //jsperf.com/cachingjq

George, 乔治,

Try this in your 'cached' case and see what the performance difference is: 在“已缓存”的情况下尝试此操作,看看性能差异是什么:

(function($) {  
    $.fn.menuManipulation = function() {
        this.parents().show();
    };
})(jQuery);

var menu = $('.menu-vertical');
$('li.selected', menu).menuManipulation();
$('li.selected > ul.static', menu).show();
$('li.static', menu).has('ul').addClass("with-child");

It's debatable as to whether find() is 'slow as hell', or infact fast as hell. 至于find()是“地狱般缓慢”还是实际上像地狱般快速存在争议。 Your performance issues could depend on the version of jQuery your using, or the structure of your DOM. 您的性能问题可能取决于您使用的jQuery版本或DOM的结构。

See here for the other side of the argument for find() performance: jQuery selector performance , http://seesparkbox.com/foundry/jquery_selector_performance_testing 有关find()性能的参数的另一面,请参见此处: jQuery选择器性能http : //seesparkbox.com/foundry/jquery_selector_performance_testing

Benchmark test: Find() Vs Selector 基准测试: Find()与选择器

Cache the elements your actually going to be working on, in your case the 'li' elements. 缓存您实际要处理的元素,在您的情况下为“ li”元素。

var menu = $('.menu-vertical li');
menu.filter('.selected').children('ul.static').show().end().menuManipulation();
menu.filter('.static').has('ul').addClass("with-child");

Your "optimized" cache version is actually less optimized because of the additional and redundant searching for the li elements. 实际上,由于对li元素进行了额外的冗余搜索,因此“优化”的缓存版本实际上未得到优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM