I have a question about DOM selectors.
Is there a performance difference between;
This code:
if (document.getElementById("post-dropdown-menu-" + postID) && document.getElementById("post-dropdown-menu-" + postID).style.display != "none") {
document.getElementById("post-dropdown-menu-" + postID).fadeOut(200);
return false;
}
And this code:
var neededElement = document.getElementById("post-dropdown-menu-" + postID);
if (neededElement && neededElement.style.display != "none") {
neededElement.fadeOut(200);
return false;
}
I really want to know if browser or javascript doing DOM Select proc. again if element already used in a function.
Of course the second one is faster because you only use query selector once which mean less processing.
and here is a test score using JSBench.
Note that the effect will never be noticed except if you are using this inside a loop or so.