documentFragment appendChild vs jquery append

JavaScript performance comparison

Revision 50 of this test case created by

Preparation code

<div id='wrapper'>
<div id="test-area" style="height: 75px; width: auto; overflow:scroll"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var inner_loop_size = 1000

};

Benchmark.prototype.teardown = function() {
  $('#test-area').empty();

};
</script>

Preparation code output

<div id='wrapper'> <div id="test-area" style="height: 75px; width: auto; overflow:scroll"></div> </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
insertAdjacentHTML string
var Area = $('#test-area');
var nodes = "";
var i = 0;
for (i; i < inner_loop_size; i++) {
  var p = $('<p/>');
  p.html("jquery elements");
  nodes += p[0].outerHTML;
}
Area[0].insertAdjacentHTML('beforeend', nodes);
pending…
append items one by one ( BAD )
var Area = $('#test-area');
var raw_fragment = document.createDocumentFragment();
var i = 0;
for (i; i < inner_loop_size; i++) {
  var p = $('<p/>');
  p.html("jquery elements");
  Area.append(p);
}
pending…
jquery.append array of jquery wrapped nodes
var Area = $('#test-area');
var nodes = [];
var i = 0;
for (i; i < inner_loop_size; i++) {
  var p = $('<p/>');
  p.html("jquery elements");
  nodes.push(p);
}
Area.append(nodes);
pending…
jquery.append array of elements
var Area = $('#test-area');
var nodes = [];
var i = 0;
for (i; i < inner_loop_size; i++) {
  var p = $('<p/>');
  p.html("jquery elements");
  nodes.push(p[0]);
}
Area.append(nodes);
pending…
jquery.append documentFragment
var Area = $('#test-area');
var raw_fragment = document.createDocumentFragment();
var i = 0;
for (i; i < inner_loop_size; i++) {
  //the inner loop use jQuery to keep it the same as in the other loops.
  var p = $('<p/>');
  p.html("jquery elements");
  raw_fragment.appendChild(p[0]);
}
Area.append(raw_fragment);
pending…
raw javascript
var Area = document.getElementById('test-area');
var raw_fragment = document.createDocumentFragment();
var i = 0;
for (i; i < inner_loop_size; i++) {
  //the inner loop use jQuery to keep it the same as in the other loops.
  var p = $('<p/>');
  p.html("jquery elements");
  raw_fragment.appendChild(p[0]);
}
Area.appendChild(raw_fragment);
pending…
.detach
var Area = $('#test-area').detach();
var i = 0;
for (i; i < inner_loop_size; i++) {
  var p = $('<p/>');
  p.html("jquery elements");
  Area.append(p);
}
$('#wrapper').append(Area);
pending…
.repalceWith
var Area = $('#test-area');
var nodes = [];
var i = 0;
for (i; i < inner_loop_size; i++) {
  var p = $('<p/>');
  p.html("jquery elements");
  nodes.push(p);
}
Area.replaceWith(Area.append(nodes));
pending…
jquery string append from test #3
var Area = $('#test-area');
var nodes = "";
var i = 0;
for (i; i < inner_loop_size; i++) {
  var p = $('<p/>');
  p.html("jquery elements");
  nodes += p[0].outerHTML;
}
Area.append(nodes);
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