I’m building one of my first web apps using HTML5, specifically targeting iPhones.
Since I’m pretty new to this, I’m trying to develop some good coding habits, follow best practices, optimize performance, and minimize the load on the resource-constrained iPhone.
One of the things I need to do frequently… I have numerous divs (each of which has a unique id) that I’m frequently updating (e.g., with innerHTML), or modifying (e.g., style attributes with webkit transitions and transforms).
In general – am I better off using getElementByID each time I need a handle to a div, or should I store references to each div I access in “global” variables at the start?
(I use “global” in quotes because I’ve really just got one truly global variable – it’s an object that stores all my “global” variables as properties).
I assume using getElementByID each time must have some overhead, since the function needs to traverse the DOM to find the div. But, I’m not sure how taxing or efficient this function is.
Using global variables to store handles to each element must consume some memory, but I don’t know if these references require just a trivial amount of RAM, or more than that.
So – which is better? Or, do both options consume such a trivial amount of resources that I should just worry about which produces more readable, maintainable code?
Many thanks in advance!