Should I cache document.getElementById() in a variable or call it everytime?
There is no single right answer to this question. It all depends on what you have to work with. If you are working with a page that has massive amount of elements in the DOM tree, it's better to cache the references and reuse them, to speed up look up time. If you are working on a small page, it's better to look up elements on the fly, and minimize memory consumption of the browser.
It also depends on the browsers you are targeting. For example, newer versions of Firefox take a while to fine an element first time, but they cache the reference internally, so next time you are going to look it up, it's going to be almost instant. IE, on the other hand, doesn't cache lookup values, but it's seek time is much faster than Firefox on the first try.
A lot of modern frameworks will cache the elements that you found for you. However, I, personally still prefer to use document.getElementById most of the time. What I do, when I need to cache lookup values is the following:
function registerElement(id)
{
if (!this["get_" + id])
this["get_" + id] = function() {
var element = document.getElementById(id);
this["get_" + id] = function() {return element;};
return element;
}
}
You use this by calling registerElement and passing it an ID of the element. When you need to retrieve the value, you call get_element id you passed and on the first run it will look up the element and cache it, on every consecutive call it will just return cached value.
You should of course reuse the reference where possible, but you might need to get a new reference in each function body.
Example:
var e1 = document.getElementById('id1');
e1.innerHTML = 'test';
e1.className = 'info';
If you keep references longer, you may find that they no longer work. If you for example get innerHTML for a part of the page and stores it back, all elements in that part is removed and recreated. If you had a reference to one of the elements in that part, that element no longer exists.
// This will recreate all elements inside the 'parent' element:
document.getElementById('parent').innerHTML += 'test';
getElementById
returns an element node, which is essentially just a JavaScript object. You can assign this object to a variable, meaning that the variable will point to this object whenever you type that variable at a later stage. So,
var id1 = document.getElementById('id1');
id1
now refers to the DOM element with an id
of id1
. If no element was found with that id
then document.getElementById
returns null.
If the elements stay within the DOM and don't get replaced then it makes sense to store them in an array, so that you can reference them as many times as you want without any performance costs.
If it helps, you could create a simple function to do it for you:
function getElementsByIds( /* id1, id2 ... */ ) {
var elems = [];
for (var i = 0, l = arguments.length; i < l; i++) {
elems[i] = document.getElementById(arguments[i]);
}
return elems;
}
app.elements = getElementsByIds('id1', 'id2', 'id3');
It's certainly faster to store the elements in a variable, but not by a large margin. This is something that's different from case to case and should be adapted for each one individually.
Maybe the biggest factor is legibility, I believe accessing the elements directly is more readable.
theMainButton.style.color = "red";
// vs.
document.getElementById("theMainButton").style.color = "red";