UIZE JavaScript Framework

PERFORMANCE TEST: getElementById vs getElementsByName

This test page demonstrates that - under the specific conditions of this test, at least - using the document.getElementsByName method is slower than using the document.getElementById method in Firefox, but is roughly as slow in Internet Explorer.

In the test, HTML for 1000 INPUT tags is generated and inserted into the page. Each INPUT tag has both id and name attributes, both having the same value that is unique to the node. Then, an entire loop through all of the nodes IDs/names is performed 40 times. In the first test, the document.getElementById method is used for every node ID/name. In the second test, the document.getElementsByName method is used instead.

A conclusion that can be drawn from the test is that, while it may seem like a neat feature of an abstraction layer to try lookup of nodes through both the document.getElementsByName and document.getElementById methods to catch nodes that have either name or id attributes, there may be a performance cost, depending on the complexity of a document's DOM and the ambient CPU load of the page.