As Carl-Andre Gagnon noted, accessing your own JavaScript resource and calling the DOM function / property are two different operations.
Some DOM properties are displayed as JavaScript properties using the DOM IDL ; this is not the same as JS special properties and requires DOM access. In addition, although DOM properties are displayed, there is no strict relationship with DOM attributes!
For example, inputElm.value = "x" will not update the DOM attribute, even if the element displays and reports the updated value. If the goal is to use DOM attributes, the only right method is to use hasAttribute/setAttribute , etc.
I am working on getting an “honest” micro-test for various function calls, but it is quite complicated, and many different optimizations are happening. Here is my best result , which I will use to argue my case.
Note that there is no if or removeAttribute to confuse the results, and I focus only on accessing the DOM / JS properties. In addition, I am trying to exclude the statement that the difference in speed is simply due to a function call, and I assign results to avoid glaring browser optimizations. YMMV.
remarks:
Access to the JS resource is fast. This is to be expected 1.2
A function call can result in a higher cost than direct access to properties 1 but not as slow as DOM properties or DOM functions. That is, it is not just a “function call” that makes hasAttribute much slower.
Access to DOM properties is slower than native access to JS properties; however, performance is very different between DOM properties and browsers. My updated micro-test shows a tendency that DOM access - whether through the DOM property or the DOM function - may be slower than native access to JS 2 properties.
And back to the very top: Access to the non-DOM [JS] property in an element is fundamentally different from access to the DOM property, and even more so to the DOM attribute in the same element. It is this fundamental difference and optimization (or lack) between approaches in browsers that explains the observed differences in performance.
1 IE 10 does some tricky tricks when calling a fake function is very fast (and I suspect the call was canceled), even though it has terrible access to JS properties. However, given that IE is a throwaway or just reinforcement, that calling a function is not something that introduces inherently slower behavior, it does not detract from my main argument: this access to the DOM is significantly lower.
2 I would like to say that access to DOM properties is slower, but FireFox does some amazing optimizations to input.value (but not img.src ). There is some special magic here. Firefox does not optimize access to DOM attributes.
And different browsers can have completely different results. However, I don’t think that you need to consider any “magic” with if or removeAttribute in order to at least isolate what I consider to be “performance”: actually using the DOM.