Kode for All – by Don Burks
Posts tagged scope
Cash in on Caching (aka The This or That Debate)
0I hope you’ve been enjoying the first three installments of 5 Things Everyone Could Learn from the jQuery Source. Here is the fourth installment.
4) Cash in on Caching (aka The This or That Debate)
Caching variables is a perfect example of one of the most common (but yet poorly-implemented) practices that JavaScript/jQuery developers do. Caching is making a copy of a variable that you are going to be referring to multiple times later in your code. This is usually done to improve efficiency. Here is an example of code that could benefit from caching:
if (document.getElementById("mainbody")) {
doSomeStuffIfItIsMain(1);
}
doSomeStuffYouAlwaysDo();
if (document.getElementById("mainbody")) {
doSomeStuffIfItIsMain(2);
}
