Appending multiple elements to the DOM

JavaScript performance comparison

Test case created by Chris Harrington

Preparation code

<div id="container"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  var COUNT = 100;

};

Benchmark.prototype.teardown = function() {
  $("#container").empty();

};
</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
DOM Updates in Loop
var container = $("#container");
for (var i = 0; i < COUNT; i++)
    container.append($("<span></span>"));
pending…
Single DOM Update with jQuery Div
var container = $("#container"), wrapper = $("<div />");
for (var i = 0; i < COUNT; i++)
    wrapper.append($("<span></span>"));
container.append(wrapper);
pending…
Single DOM Update with Document Fragment
var container = $("#container"), fragment = document.createDocumentFragment();
for (var i = 0; i < COUNT; i++)
    fragment.appendChild($("<span></span>")[0]);
container.append(fragment);
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