String concatenation vs document.createDocumentFragment()

JavaScript performance comparison

Revision 11 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 header = 'header test';
    var body = 'body text lorem ipsum';
    var obj = {
      header: 'header test',
      body: 'body text lorem ipsum'
    };
    var doc = window.document;
    var body = doc.getElementById('body');
   
};

Benchmark.prototype.teardown = function() {
    body.innerHTML = '';
    html = '';
};
</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
(function() {
html = '<div class="container">'
html += '<h1>' + header + '</h1>';
html += '<div class="content">' + body + '</div>';
html += '</div>';
var range = doc.createRange();
range.setStart(body, 0);
range.collapse(false);
body.appendChild(range.createContextualFragment(html));
})();
pending…
fragment
(function() {
var frag = doc.createDocumentFragment();
var h1 = doc.createElement('h1');
h1.text = obj.header;
var content = doc.createElement('div');
content.className = 'content';
content.text = obj.body;
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