innerHtml vs. hide

JavaScript performance comparison

Test case created by Mahrew Lugmatur

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<div id="container">
</div>

<style>
.hidden {display: none}
</style>
<script>
Benchmark.prototype.setup = function() {
    var elementA = '<p class="a">Lorem ipsum dolor isamet</p>'
    var elementB = '<p class="b">Lorem ipsum dolor isamet</p>'
   
    $('#container').html(elementA + elementB)
};
</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
innerHtml (DOM offloading)
$('#container').html(elementA)

$(elementB).addClass('huhu');
$(elementB).html('test');
$(elementB).html('test2');
$(elementB).html('test3');
$(elementB).removeClass('huhu');
$(elementB).html('test');
$(elementB).html('test2');
$(elementB).html('test3');
$(elementB).addClass('huhu');
$(elementB).html('test');
$(elementB).html('test2');
$(elementB).html('test3');

$('#container').html(elementB)
pending…
hide
$('p.a').removeClass('hidden')
$('p.b').addClass('hidden')

$('p.b').addClass('huhu');
$('p.b').html('test');
$('p.b').html('test2');
$('p.b').html('test3');
$('p.b').removeClass('huhu');
$('p.b').html('test');
$('p.b').html('test2');
$('p.b').html('test3');
$('p.b').addClass('huhu');
$('p.b').html('test');
$('p.b').html('test2');
$('p.b').html('test3');

$('p.a').addClass('hidden')
$('p.b').removeClass('hidden')
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