DOM vs innerHTML (v27)

Revision 27 of this benchmark created on


Preparation HTML

<div id='sample'>
  <h1 id='title'>Title</h1>
  <h2 id='subtitle'>by</h2>
  <div id='content'>
  </div>
</div>

Setup

var colors = ["red", "yellow", "green", "cyan", "blue", "magenta"];

Teardown


    var sample = document.getElementById('sample');
    sample.innerHTML = "<h1 id='title'>Title</h1><h2 id='subtitle'>by</h2><div id='content'></div>";
  

Test runner

Ready to run.

Testing in
TestOps/sec
DOM
var content = document.getElementById('content');

var list = document.createElement('ul');
for (var i = 0, l = colors.length; i < l; ++i) {
  var item = document.createElement('li');
  item.className = "item";
  item.appendChild(document.createTextNode(colors[i]));
  list.appendChild(item);
}

content.appendChild(list);
ready
innerHTML

var list = "<ul>";
for (var i = 0, l = colors.length; i < l; ++i) {
  list += "<li class='item'>" + colors[i] + "</li>";
}
list += "</ul>";

var content = document.getElementById('content');
content.innerHTML = list;
ready

Revisions

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