Cache objects repeatedly accessed by JavaScript


Accessing the DOM (Document Object Model) drains processor resources (CPU). Therefore, when you reference the same DOM element several times in JavaScript, store its reference within a variable to avoid re-parsing the DOM for the same element.

Instead of:

document.getElementById('menu').property1 = 'foo';
document.getElementById('menu').property2 = 'bar';


$menu = document.getElementById('menu');
$menu.property1 = 'foo';
$menu.property2 = 'bar';
This best practice should only be applied if it is coherent with your project's specifications.
Under CC-By-NX-SA license