innerHTML vs createElement test

JavaScript performance comparison

Revision 60 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="container">
</div>
      
<script>
Benchmark.prototype.setup = function() {
  document.getElementById('container').innerHTML = '';
  
  function outerHTML(node){
      return node.outerHTML || new XMLSerializer().serializeToString(node);
  }

};

Benchmark.prototype.teardown = function() {
  document.getElementById('container').innerHTML = '';

};
</script>

Preparation code output

<div id="container"> </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
100 element bulk createElement + innerHTML + outerHTML
var container = document.getElementById('container');
var outerdiv = document.createElement('div');


for (var i = 0; i < 100; i++) {
  var div = document.createElement('div');
  var span = document.createElement('span');

  span.innerHTML = i;
  div.appendChild(span);
  outerdiv.appendChild(div);

}

container.innerHTML = outerHTML(outerdiv);
pending…
100x insertAdjacentHTML
var i = -1, div = document.createElement('div'), html='<div>';

while (++i < 100) {
    html += '<div><span>' + i + '</span></div>';
}

html += '</div>';

document.getElementById('container').insertAdjacentHTML('afterbegin', html);
pending…
100 element innerHTML
var innerHTMLstr = '<div>';
for (var i = 0; i < 100; i++) {
  innerHTMLstr += '<div><span>' + i + '</span></div>';
}
innerHTMLstr+='</div>';

document.getElementById('container').innerHTML = innerHTMLstr;
pending…
100 element createElement added in sequence
var container = document.getElementById('container');
var outerdiv = document.createElement('div');
container.appendChild(outerdiv);

for (var i = 0; i < 100; i++) {
  var div = document.createElement('div');
  var span = document.createElement('span');

  span.innerHTML = i;
  div.appendChild(span);
  outerdiv.appendChild(div);

}
pending…
100 element createElement bulk add
var container = document.getElementById('container');
var outerdiv = document.createElement('div');

for (var i = 0; i < 100; i++) {
  var div = document.createElement('div');
  var span = document.createElement('span');

  span.innerHTML = i;
  div.appendChild(span);
  outerdiv.appendChild(div);

}

container.appendChild(outerdiv);
pending…
100 element innerHTML with Array.join()
var innerHTMLarray = [ '<div>' ];
for (var i = 0; i < 100; i++) {
  innerHTMLarray[innerHTMLarray.length] = '<div><span>';
  innerHTMLarray[innerHTMLarray.length] = i;
  innerHTMLarray[innerHTMLarray.length] = '</span></div>';
}
innerHTMLarray[innerHTMLarray.length] = '</div>';

document.getElementById('container').innerHTML = innerHTMLarray.join('');
pending…
100 element innerHTML with Array.join() and Array.push()
var innerHTMLarray = [ '<div>' ];
for (var i = 0; i < 100; i++) {
  innerHTMLarray.push('<div><span>', i, '</span></div>');
}
innerHTMLarray.push('</div>');

document.getElementById('container').innerHTML = innerHTMLarray.join('');
pending…
100 element createElement + innerHTML + outerHTML
var container = document.getElementById('container');

for (var i = 0; i < 100; i++) {
  var div = document.createElement('div');
  var span = document.createElement('span');

  span.innerHTML = i;
  div.appendChild(span);
  container.innerHTML += outerHTML(div);

}

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