I repetitively use document.getElementById
a lot on common CSS elements.
Would there be a significant performance gain if I created a global array
to store all of my document.getElementById
element in instead of refetching the element each time?
Example, instead of:
document.getElementById("desc").setAttribute("href", "#");
document.getElementById("desc").onclick = function() {...};
document.getElementById("desc").style.textDecoration = "none"
document.getElementById("asc").setAttribute("href", "#");
document.getElementById("asc").onclick = function() {...};
document.getElementById("asc").style.textDecoration = "none"
...
To simply do:
var GlobalElementId = [];
GlobalElementId ["desc"] = document.getElementById("desc");
GlobalElementId ["asc"] = document.getElementById("asc");
GlobalElementId [...] = document.getElementById(...);
GlobalElementId ["desc"].setAttribute("href", "#");
GlobalElementId ["desc"].onclick = function() {...};
GlobalElementId ["desc"].style.textDecoration = "none"
...
#
to the id to get a selector string (and maybe escaping the ID if it contains a:
or.
), and then jQuery has to turn that selector back into a call to getElementById and pack the result in a jQuery wrapper... Chaos is right. I'm afraid any question you ask here with JavaScript in it will usually be flooded with blather from the SO “use jQuery! It's so brilliant!!” mafia, regardless of whether jQuery is any help at all in the given situation. – Typicalvar
is?! – Fazio