String concatenation vs document.createDocumentFragment()

JavaScript performance comparison

Revision 12 of this test case created

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<div id="body">
</div>
<script>
Benchmark.prototype.setup = function() {
    var html;
    var obj = {
      header: 'header test',
      body: 'body text lorem ipsum'
    };
    var doc = window.document;
    var body = doc.getElementById('body');
   
};

Benchmark.prototype.teardown = function() {
    html = '';
    $(body).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
String
body.innerHTML = '';
html = '<div class="container">'
html += '<h1>' + obj.header + '</h1>';
html += '<div class="content">' + obj.body + '</div>';
html += '</div>';
body.innerHTML = html;
pending…
fragment
body.innerHTML = '';
var frag = doc.createDocumentFragment();
var container = doc.createElement('div');
container.className = 'container';
var h1 = doc.createElement('h1');
h1.textContent = obj.header;
container.appendChild(h1)
var content = doc.createElement('div');
content.className = 'content';
content.textContent = obj.body;
container.appendChild(content);
frag.appendChild(container);
body.appendChild(frag);
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