nextSibling vs childNodes (v15)

Revision 15 of this benchmark created by Yanis Benson on


Description

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

Preparation HTML

<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>

Test runner

Ready to run.

Testing in
TestOps/sec
childNodes
var len = container.childNodes.length,
    i;
for (i = len; i >= 0; i--) {
 process(container.childNodes[i]);
}
ready
childNodes, cached, reversed
var nodes = container.childNodes,
    len = nodes.length,
    i;
for (i = len; i--; ) {
 process(nodes[i]);
}
ready
nextSibling
var node = container.firstChild;
while (node != null) {
 process(node);
 node = node.nextSibling;
}
ready
previousSibling
var node = container.lastChild;
while (node != null) {
 process(node);
 node = node.previousSibling;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.