innerHTML vs createElement test

JavaScript performance comparison

Revision 56 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 = '';

};

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
Jquery appendTo
var container = $("#container");
var outerdiv = $("<div>");

for (var i = 0; i < 100; i++) {
  var div = $("<div>");
  var span = $("<div>");

  //span.text(i);
  span.appendTo(div);
  div.appendTo(outerdiv)

}

container.append(outerdiv);
pending…
Jquery appendTo[0]
var container = $("#container");
var outerdiv = $("<div>");

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

  //span.text(i);
  div[0].appendChild(span[0]);
  outerdiv[0].appendChild(div[0])

}

  container[0].appendChild(outerdiv[0]);
pending…
100 element innerHTML
var innerHTMLstr = '<div>';
for (var i = 0; i < 100; i++) {
  innerHTMLstr += '<div><span></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…

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