Moving children

JavaScript performance comparison

Revision 2 of this test case created by

Preparation code

<div id="anchor"></div>
      
<script>
Benchmark.prototype.setup = function() {
  var anchor = document.getElementById('anchor');
  var parent = anchor.parentNode;
  
  parent.innerHTML = '<div id="anchor"></div>';
  
  var elems = [ ];
  
  elems[0] = document.createElement('div');
  elems[0].id = 'dest';
  
  elems[1] = document.createElement('div');
  elems[1].id = 'source';
  
  elems[2] = document.createElement('div');
  elems[2].innerHTML = 'a child';
  
  elems[3] = document.createElement('div');
  elems[3].innerHTML = 'another child';
  
  elems[4] = document.createElement('ul');
  
  elems[5] = document.createElement('li');
  elems[5].innerHTML = 'a list  item';
  
  elems[6] = document.createElement('li');
  elems[6].innerHTML = 'another list item';
  
  elems[1].appendChild(elems[2]);
  elems[1].appendChild(elems[3]);
  elems[1].appendChild(elems[4]);
  
  elems[4].appendChild(elems[5]);
  elems[4].appendChild(elems[6]);
  
  parent.appendChild(elems[0]);
  parent.appendChild(elems[1]);
  
  var source = elems[1];
  var dest = elems[0];

};
</script>

Preparation code output

<div id="anchor"></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
innerHTML
dest.innerHTML = source.innerHTML;
source.innerHTML = '';
pending…
appendChild
while (source.children.length) {
	dest.appendChild(source.children[0]);
}
pending…
innerHTML + removeChild
dest.innerHTML = source.innerHTML;

while (source.children.length) {
	source.removeChild(source.children[0]);
}
pending…
appendChild + removeChild
while (source.children.length) {
  var elem = source.children[0];
  source.removeChild(elem);
	dest.appendChild(elem);
}
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.

0 Comments