jQuery: create element

JavaScript performance comparison

Test case created by Dmitrij

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="cntr"></div>
<script>
Benchmark.prototype.setup = function() {
    var $eCntr = $('div#cntr'),
    nCnt = 10;
   
    function drawItem(pI) {
        var eC = document.createElement('div');
        eC.className = 'classDiv';
        eC.setAttribute('attr1', 'somevalue'+pI);
   
        var eP = document.createElement('p');
        eP.className = 'classP';
   
        var txt = pI+' Lorem ipsum dolor sit amet, consectetur adipiscing elit. ';
   
        eP.appendChild(document.createTextNode(txt));
    //  var propTxt = (('innerText' in eP)? 'innerText' : 'textContent');
    //  eP[propTxt] = txt;
    /*
        if (eP.textContent) {
                eP.textContent = txt;
        } else
                eP.innerText = txt;
    */

    //   eP.innerHtml = txt;
        eC.appendChild(eP);
   
        return eC;
    }
   
    function drawItem2(pI) {
    return '<div class="classDiv" attr1="someValue'+pI+'"><p class="classP">'+ pI +' Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></div>';
    }
   
};

Benchmark.prototype.teardown = function() {
    $eCntr.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
$(native)
var eCntr = $eCntr[0],
        eFrgm = document.createDocumentFragment();

for (var i=1; i<=nCnt; i++) {
        eFrgm.appendChild(drawItem(i));
}
eCntr.appendChild(eFrgm);
pending…
$.html
var html ='';
for (var i=1; i<=nCnt; i++) {
        html += drawItem2(i);
}

var eCntr = $eCntr[0];
eCntr.innerHTML = html;
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