简体   繁体   English

jQuery或原始JavaScript是否预编译或缓存变量表达式或选择器?

[英]Do jQuery or raw JavaScript precompile or cache variable expressions or selectors?

Pretty basic question. 很基本的问题。

I want to know if jQuery or raw JavaScript are smart enough to precompile, or cache variable expressions in functions, or selectors, and if not, are there any reasons that would stop them doing this? 我想知道jQuery或原始JavaScript是否足够聪明以进行预编译,或在函数或选择器中缓存变量表达式,如果没有,是否有任何原因会阻止它们执行此操作?

eg 例如

$('#whatever').on('click', function(){
    $('#template').click();
});

var n = 0;

for(var i = 0; i < 20; i++){
    n = 1 + 1;
    $('#whatever').click();
}

console.log(n);

Are we going to search the dom 20 times for the $('#template') element? 我们是否要在dom中搜索$('#template')元素20次? or are the library/engine smart enough to store a pointer? 还是库/引擎足够智能以存储指针?

And, are we going to computer 1+1 2 times, or is that expression cached or precompiled? 而且,我们要去1 + 1 2次计算机,还是该表达式被缓存或预编译?


Obviously these are really dumb examples, but they get the point across. 显然,这些确实是愚蠢的例子,但可以理解这一点。

Yes, no one is going to have the line (hopefully) n = 1 + 1 in their code. 是的,没有人希望他们的代码中有这行(希望) n = 1 + 1

And we could change the code to have `var template = $('#template'); 我们可以将代码更改为具有var template = $('#template'); before the click handler if it doesn't deal with it itself, but I am wondering do we/ why we have to do that? 在单击处理程序之前,如果它本身不能处理,但是我想知道我们/为什么必须这样做?

First off, jQuery is javascript. 首先,jQuery javascript。 jQuery does not and cannot add features to JavaScript as a language. jQuery不会并且不能将功能作为语言添加到JavaScript。

With that being said, JavaScript is ECMAScript, and also implemented differently in every browser. 话虽这么说,JavaScript是ECMAScript,并且在每个浏览器中都以不同的方式实现。 It would be very difficult (if not impossible) for me to tell you anything about what level of caching a JavaScript implementation has, because as a language, there isn't any prescribed behavior. 我很难(如果不是不可能)告诉您有关JavaScript实现的缓存级别的任何信息,因为作为一种语言,没有任何规定的行为。

I think it would make sense to cache references to DOM elements for performance reasons, but I cannot speak to what each separate implementation does behind the scenes. 我认为出于性能原因缓存对DOM元素的引用是有意义的,但是我无法说说每个单独的实现在后台所做的事情。

As far as jQuery is concerned, there is no magical selector caching happening in the current version as far as I'm aware. 就jQuery而言,据我所知,在当前版本中没有神奇的选择器缓存发生。 Every time $(...) is called, it has to re-evaluate the selector. 每次调用$(...) ,都必须重新评估选择器。 The selector could be a simple css-style selector, ex $('#foo .bar baz') , or it could be an element fragment, ex $('<div>foo bar baz</div>') . 选择器可以是简单的css样式选择器,例如$('#foo .bar baz') ,也可以是元素片段,例如$('<div>foo bar baz</div>')

If the jQuery function is passed a selector, it would have to re-check the DOM because new elements may have been added that could match the selector, or existing elements may have changed to no longer match the selector. 如果将jQuery函数传递给选择器,则必须重新检查DOM,因为可能添加了可能与选择器匹配的新元素,或者现有元素可能已更改为不再与选择器匹配。

As far as caching jQuery objects goes, it is common to save a reference to a jQuery object, and in some circumstances is more performant. 就缓存jQuery对象而言,通常保存对jQuery对象的引用,并且在某些情况下会提高性能。 The only way to know for sure which method is better is to run the script with a performance analyzer and see where the bottlenecks are. 唯一确定哪种方法更好的唯一方法是使用性能分析器运行脚本,并查看瓶颈在哪里。 There's no reason to just assume that writing your code in one particular way will improve the performance of your code. 没有理由仅仅假设以一种特定的方式编写代码可以提高代码的性能。

If you're just trying to perform a series of operations on the same selection, be sure to take advantage of jQuery's method chaining: 如果您只是想对同一选择执行一系列操作,请确保利用jQuery的方法链接:

$('.selector').addClass('foo').removeClass('bar').click(function () {...});

jQuery methods typically return the original selection as a return value, which allows for a very elegant simplification of code. jQuery方法通常将原始选择作为返回值返回,这可以非常优雅地简化代码。

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

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