Create element vs set HTML

JavaScript performance comparison

Test case created by zcrar70

Preparation code

<div id="test"><div>old content</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var existing = $('#test').children()[0];
    var parent = $('#test')[0];
   
    var renderNewElement = function() {
        var e = document.createElement('div');
        e.innerHTML = "<div>some content</div>";
        parent.replaceChild(e, existing);
        existing = e;
    };
   
    var renderInnerHTML = function() {
        parent.innerHTML = "<div>some content</div>";
        }
};
</script>

Preparation code output

old content

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
New element
renderNewElement();
pending…
InnerHTML
renderInnerHTML();
pending…

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

Compare results of other browsers

0 comments

Add a comment