documentFragment appendChild vs jquery append

JavaScript performance comparison

Revision 17 of this test case created

Preparation code

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

Benchmark.prototype.teardown = function() {
    $test_area.empty();
};
</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
insert document fragment
var fragment = document.createDocumentFragment();

for (var i = 0; i < 1000; i++) {
  var p = document.createElement('p');
  p.appendChild(document.createTextNode('The quick brown fox jumps over the lazy dog'));
  fragment.appendChild(p);
}

test_area.appendChild(fragment);
pending…
direct insert
for (var i = 0; i < 1000; i++) {
  var p = document.createElement('p');
  p.appendChild(document.createTextNode('The quick brown fox jumps over the lazy dog'));
  test_area.appendChild(p);
}
pending…
jQuery insert div
var div = $('<div>');

for (var i = 0; i < 1000; i++) {
  var p = $('<p>');
  p.append('The quick brown fox jumps over the lazy dog');
  div.append(p);
}

// not functionally equivalent to other snippets
$test_area.append(div);
pending…
jQuery direct insert
for (var i = 0; i < 1000; i++) {
  var p = $('<p>');
  p.append('The quick brown fox jumps over the lazy dog');
  $test_area.append(p);
}
pending…
jQuery direct insert & raw create
for (var i = 0; i < 1000; i++) {
  var p = document.createElement('p');
  p.appendChild(document.createTextNode('The quick brown fox jumps over the lazy dog'));
  $test_area.append(p);
}
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