innerHTML vs removeChild

JavaScript performance comparison

Revision 460 of this test case created by Pascal Wacker

Preparation code

<div id='box'></div>
      
<script>
Benchmark.prototype.setup = function() {
  function showUrl(url) {
  }
  
  function createLink(url) {
      var link = document.createElement('div');
      link.onclick = function () { showUrl(url); };
      link.title = url;
      return link;
  }
  
  function addItem(content, url)
  {
   var text_link = createLink(url);
   content.appendChild(text_link);
  }
  
  var box = document.getElementById('box');
  for (var i = 0; i < 100; i++) 
  {
   addItem(box, i + '-url');
  }

};

Benchmark.prototype.teardown = function() {
  for (var i = 0; i < 100; i++) 
  {
   addItem(box, i + '-url');
  }

};
</script>

Preparation code output

<div id='box'></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
box.innerHTML = '';
pending…
removeChild
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
pending…

Revisions

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

0 Comments