nextSibling vs childNodes

JavaScript performance comparison

Revision 9 of this test case created by Sam3000

Info

Test the relative performance of iterating through child nodes using nextSibling vs iterating through the childNodes array.

Preparation code

<div id="container">
</div>
<script>
  var container = document.getElementById('container');
  var s = "";
  for (var i=0; i<100; ++i) s+="<p>Child</p>";
  container.innerHTML = s;
 
  function process(element) {}
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
childNodes
var len = container.childNodes.length,
    i;
for (i = 0; i < len; i++) {
 process(container.childNodes[i]);
}
pending…
childNodes, cached, reversed
var nodes = container.childNodes,
    len = nodes.length,
    i;
for (i = len; i--; ) {
 process(nodes[i]);
}
pending…
nextSibling
var node = container.firstChild;
while (node) {
 process(node);
 node = node.nextSibling;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment