createDocumentFragment vs appendChild on non-appended element

JavaScript performance comparison

Revision 11 of this test case created by Lonnen

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var form = document.createElement('form'),
        array = [],
        iterations = 400,
        i;
   
    for (i = 0; i < iterations; i += 1)
    {
        array.push(document.createElement('input'))
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Normal Append
for (i = 0; i < iterations; i +=1)
{
    form.appendChild(array[i]);
}

document.body.appendChild(form);
document.body.offsetHeight;
document.body.removeChild(form);
pending…
Document Fragment
var formDiv = document.createDocumentFragment();

for (i = 0; i < iterations; i +=1)
{
    formDiv.appendChild(array[i]);
}
   
form.appendChild(formDiv);
document.body.appendChild(form);
document.body.offsetHeight;
document.body.removeChild(form);
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