String concatenation vs document.createDocumentFragment()

JavaScript performance comparison

Revision 16 of this test case created by oahrens

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
html = '<div class="container">'
html += '<h1>' + obj.header + '</h1>';
html += '<div class="content">' + obj.body + '</div>';
html += '</div>';
body.innerHTML = html;
pending…
fragment
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;

frag.appendChild(h1);
frag.appendChild(content);

body.appendChild(frag);
pending…
joined Array
html = [];
html.push('<div class="container">');
html.push('<h1>');
html.push(obj.header);
html.push('</h1>');
html.push('<div class="content">');
html.push(obj.body);
html.push('</div>');
html.push('</div>');
body.innerHTML = html.join('');
pending…
jQuery
container = $('<div>').addClass('container');
h1 = $('<h1>').text(obj.header).appendTo(container);
content = $('<div>').addClass('content').html(obj.body).appendTo(container);
container.appendTo(body);
pending…
divFragment
var html = '<div class="container">'
html += '<h1>' + obj.header + '</h1>';
html += '<div class="content">' + obj.body + '</div>';
html += '</div>';

var frag = doc.createDocumentFragment();
var div = doc.createElement('div');
div.innerHTML = html;
frag.appendChild(div);
body.appendChild(frag);
pending…
arrayJoinDivFragment
html = [];
html.push('<div class="container">');
html.push('<h1>');
html.push(obj.header);
html.push('</h1>');
html.push('<div class="content">');
html.push(obj.body);
html.push('</div>');
html.push('</div>');

var frag = doc.createDocumentFragment();
var div = doc.createElement('div');
div.innerHTML = html;
frag.appendChild(div);
body.appendChild(frag);
pending…
divFragmentExtra
var html = '<div class="container">'
html += '<h1>' + obj.header + '</h1>';
html += '<div class="content">' + obj.body + '</div>';
html += '</div>';

var frag = doc.createDocumentFragment();
var div = doc.createElement('div');
div.innerHTML = html;
frag.appendChild(div);
body.appendChild(frag);
pending…
div
var div = doc.createElement('div');
var h1 = doc.createElement('h1');
h1.text = obj.header;
var content = doc.createElement('div');
content.className = 'content';
content.text = obj.body;

div.appendChild(h1);
div.appendChild(content);

body.innerHTML = div.innerHTML;
pending…
fragment via div
var frag = doc.createDocumentFragment();
var h1 = doc.createElement('h1');
var div = doc.createElement('div');
h1.text = obj.header;
var content = doc.createElement('div');
content.className = 'content';
content.text = obj.body;

frag.appendChild(h1);
frag.appendChild(content);

body.innerHTML = div.appendChild(frag).innerHTML;
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