nextSibling vs childNodes (v17)

Revision 17 of this benchmark created by askd 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) {return element}
</script>

Setup

var i = 20;

Test runner

Ready to run.

Testing in
TestOps/sec
childNodes
var d = process(container.childNodes[i]),
  d = 0;
ready
childNodes, cached
var nodes = container.childNodes,
  d = process(container.childNodes[i]),
  d = 0;
ready
nextSibling
var node = container.firstChild;
while (i > -1) {
  i--;
  node = node.nextSibling;
}
var d = process(node),
  d = 0;
ready
Cloned children
var nodes = Array.prototype.slice.call(container.childNodes),
  d = process(nodes[i]),
  d = 0;
ready

Revisions

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