What is the difference between using document.head
and using document.getElementsByTagName(\"head\")[0]
? Tests I ran showed that they both take about a
Using the ||
operator like that is a form of feature detection. When used, if the first value is undefined it sends back the latter value.
So for
document.head || document.getElementsByTagName("head")[0];
the reason is that if document.head
is not supported at least the right value is returned.
As for your speed test, a millisecond is a long time. I doubt it really took that long. In fact, I made a jsPerf for this. It shows that the getElementsByTagName
function is roughly 80% slower.