JavaScript Is assigning an HTML element to a variable have any particular advantage in Javascript?

Consider the two sections of Javascript code, which accomplish the exact same thing:

Version 1:

document.getElementById("test").style.backgroundColor = "green";
document.getElementById("test").style.borderColor = "red";
document.getElementById("test").style.borderStyle = "dashed";
document.getElementById("test").style.borderWidth = "thin";
document.getElementById("test").style.borderStyle = "dashed";
document.getElementById("test").style.margin = "0";
document.getElementById("test").style.padding = "0";

Version 2:

var testDiv = document.getElementById("test");
testDiv.style.backgroundColor = "green";
testDiv.style.borderColor = "red";
testDiv.style.borderStyle = "dashed";
testDiv.style.borderWidth = "thin";
testDiv.style.borderStyle = "dashed";
testDiv.style.margin = "0";
testDiv.style.padding = "0";

Putting aside which one takes longer to type, or any other human input or readability issues, is either one more or less efficient than the other? Is there anything that differentiates them other than how they look to a human? Put another way, is there any reason other than one's personal coding style to choose one over the other?

Answer:1

Each call to getElementById() will force the browser to perform another lookup in the DOM, which is a significant performance hit compared with following a direct reference.

You can test things like this by making a JSPerf - here's one for your question that shows a perf gain (at least on my browser) from reusing the looked-up DOM node.

Answer:2

Yes, you can speed up your code (although, probably not by that much) if you only do the getElementById() call once, and assign it to a variable. getElementById() is a function, after all, and needs to be evaluated with each use.

Answer:3

Storing it in a variable is much more efficient since you won't have to rerun the document.getElemebtById() function in the browser. You are essentially caching the result. This can make a big difference in certian cases if it's slow to locate the element.

This will not be as obvious with getElementById as it's already very fast. However with certain complex JQuery selectors the difference will be very noticable in many cases

Answer:4

jquery click not working. Although, this issue has been discussed a lot, I can not find an answer that solves the problem for me. I have enclosed jQuery code in $(document).ready but vainly. here is ...

jquery click not working. Although, this issue has been discussed a lot, I can not find an answer that solves the problem for me. I have enclosed jQuery code in $(document).ready but vainly. here is ...

  1. jquery click does not work
  2. jquery click does nothing
  3. jquery click does not fire
  4. jquery trigger click does not work
  5. jquery click event does not work
  6. jquery button click does not work
  7. jquery click event does not bind
  8. jquery click doesn't work on mobile
  9. jquery checkbox click does not work

I need to use Firefox web console a lot and I want every time I open Firefox, the Web Console panel will display automatically. Currently I have to press Ctrl + Shift + K to open it. Is there any ...

I need to use Firefox web console a lot and I want every time I open Firefox, the Web Console panel will display automatically. Currently I have to press Ctrl + Shift + K to open it. Is there any ...

  1. open firefox console
  2. open firefox console linux
  3. firefox open console shortcut
  4. open console firefox android
  5. open console firefox mobile
  6. open console firefox mac
  7. firefox open console in new window
  8. firefox open console hotkey
  9. firefox open console log
  10. firefox open console tab
  11. firefox open console error
  12. open firefox developer console
  13. open firefox web console
  14. open javascript console firefox
  15. open browser console firefox
  16. open dev console firefox
  17. firefox open debug console
  18. firefox open java console
  19. selenium firefox open console
  20. open js console firefox

I have two lists: the first one is displayed on the left and the second on the right. I want that if I click on an element in the left, he is removed and he is prepended to the right list (and the ...

I have two lists: the first one is displayed on the left and the second on the right. I want that if I click on an element in the left, he is removed and he is prepended to the right list (and the ...

  1. jquery move element to another div
  2. jquery move element up
  3. jquery move element to new parent
  4. jquery move element after another
  5. jquery move element before another
  6. jquery move element position
  7. jquery move element out of parent
  8. jquery move element to top
  9. jquery move element on scroll
  10. jquery move element left
  11. jquery move element to body
  12. jquery move element up one level
  13. jquery move element before sibling
  14. jquery move element to end of container
  15. jquery move element to top of list
  16. jquery move element to end of parent
  17. jquery move element above sibling
  18. jquery move element to top of parent
  19. jquery move element on page
  20. jquery move element above another

I'm trying to have my map zoom or expand to fit all markers when it updates as some are plotted off the screen and you can't see them. My code is as follows... $scope.loadMap = function () { ...

I'm trying to have my map zoom or expand to fit all markers when it updates as some are plotted off the screen and you can't see them. My code is as follows... $scope.loadMap = function () { ...