Does the performance cost of searching for the same query selector within a document add up to a significant one, or does some code in the pipeline -- from the JavaScript library in use (e.g. jquery) or the JavaScript engine or the browser engine -- cache query selectors.
$(document).ready(function() {
var foo = $("#foo");
doThis();
doThat();
thenDoThat();
...
function doThis() {
$("#foo")...
...
}
function doThat() {
$("#foo")...
...
}
function thenDoThat() {
$("#foo")...
...
}
...
});
I do understand the lifetime of objects esp. in a garbage collected environment. So, the reply that the scope of the object will determine its life-time will not quite satisfy my curiosity, as that fact is obvious.
My question really is, if I accessed $("#foo")
over and over again,
will that add up to a significant CPU time and so will caching it in my code at an appropriate level obviate such cost, or
some code already caches it, or
is it totally negligible no matter how many times done?