DocumentFragment vs. String Concat

Benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://tryhandlebarsjs.com/js/libs/handlebars-1.0.0.beta.6.js"></script>

<script type="text/tpl" id="enrollment-tpl">
  <li class="user" data-id="{{id}}">{{name}}</li>
</script>
<ul id="user-list"></ul>

Setup

var enrollments = [];
    var tplSource   = $('#enrollment-tpl').html();
    var tpl         = Handlebars.compile(tplSource);
    var list        = $('#user-list');
    
    for (var i=0; i<500; i++) {
      enrollments.push($(tpl({id: i, name: 'Enrollment' + i})));
    };

Test runner

Ready to run.

Testing in
TestOps/sec
document fragment
var f = document.createDocumentFragment();
for (var i=0; i<500; i++) {
  f.appendChild(enrollments[i][0]);
};
list.append($(f));
ready
string
var string = '';
for (var i = 0; i < 500; i++) {
  string = string + enrollments[i].html();
}

list.append(string);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.