document.getElementsByTagName('a').item(0)
and
document.getElementsByTagName('a')[0]
will return the same result...
Is the former faster than the latter, maybe someone knows of test results?
document.getElementsByTagName('a').item(0)
and
document.getElementsByTagName('a')[0]
will return the same result...
Is the former faster than the latter, maybe someone knows of test results?
Selfmade performance test: http://jsfiddle.net/438jh/2/
The difference seems to be negligible. The second method performs better in most case, but if you have a look on how often the loop is performed it does not really matter.
Chrome:
item function is a member of NodeList
DOM object. NodeLists
are array-like but they are not real arrays (e.g.: they are live, read-only, lack of array functions).
The performance difference should be negligible.
For a more practical example, this technique has the best performance. Look at work by Nicholas Zakas, YAHOO! for more examples:
var cachedDOMquery = Array.prototype.slice.apply(document.getElementsByTagName('a')),
i = cachedDOMquery.length,
item;
while(i--){
item = cachedDOMquery[i];
alert(item.href);
}