createDocumentFragment vs appendChild on non-appended element

JavaScript performance comparison

Revision 24 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Normal Append
var div = document.createElement('div');
document.body.appendChild(div);

for (var i = 0; i < 1000; i++) {
  var n = document.createElement("div");
  n.innerHTML = "Hello";
  div.appendChild(n);
}

document.body.removeChild(div);
pending…
Document Fragment
var div = document.createElement('div');
document.body.appendChild(div);

var frag = document.createDocumentFragment();

for (var i = 0; i < 1000; i++) {
  var n = document.createElement("div");
  n.innerHTML = "Hello";
  frag.appendChild(n);
}

div.appendChild(frag);

document.body.removeChild(div);
pending…
Normal Append Last
var div = document.createElement('div');

for (var i = 0; i < 1000; i++) {
  var n = document.createElement("div");
  n.innerHTML = "Hello";
  div.appendChild(n);
}

document.body.appendChild(div);
document.body.removeChild(div);
pending…
jQuery Append
var div = $('<div/>');
$('body').append(div);

for (var i = 0; i < 1000; i++) {
  div.append($('<div>Hello</div>'));
}

div.remove();
pending…
InnerHTML Naive Append
var div = document.createElement('div');
document.body.appendChild(div);

for (var i = 0; i < 1000; i++) {
  div.innerHTML += "<div>Hello</div>";
}

document.body.removeChild(div);
pending…
InnerHTML Optimized Append
var div = document.createElement('div');
document.body.appendChild(div);

var html = ""
for (var i = 0; i < 1000; i++) {
  html += "<div>Hello</div>";
}
div.innerHTML += html;
document.body.removeChild(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. Here’s a list of current revisions for this page:

0 comments

Add a comment