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?
e.g.
$('#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? 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?
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.
And we could change the code to have `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?