Various methods of hiding content after DOM ready

JavaScript performance comparison

Test case created by BrynM

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>
.container {
    height: 200px;
    overflow: hidden;
}
.hidden {
   display: none;
}
</style>
<div class="container">
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
    <p>some content</p>
</div>
<script>
Benchmark.prototype.setup = function() {
    var thePsJ = $('p')
        , thePs = document.getElementsByTagName( 'p' )
        , noOp = null;
};

Benchmark.prototype.teardown = function() {
    $('p').removeClass( 'hidden' ).css( 'display', 'block' );
};
</script>

Preparation code output

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some content

some 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
$().addClass
$(document).ready( function () {
    thePsJ.addClass( 'hidden' );
});
pending…
$().hide()
$(document).ready( function () {
    thePsJ.hide();
});
pending…
$().css() (strings)
$(document).ready( function () {
    thePsJ.css( 'display', 'none' );
});
pending…
$().css() (object)
$(document).ready( function () {
    thePsJ.css( { 'display' : 'none' } );
});
pending…
native element.class +=
$(document).ready( function () {
    for ( var i = 0; i < thePs.length; i++ ) {
        thePs[i].class += 'hidden';
    }
});
pending…
native element.style.attribute =
$(document).ready( function () {
    for ( var i = 0; i < thePs.length; i++ ) {
        thePs[i].style.display = 'none';
    }
});
 
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