Batched Reflows

JavaScript performance comparison

Revision 2 of this test case created

Info

When making a number of style changes that trigger reflow, is it better to hide the element before making the changes, and then show it again after the changes are complete?

Preparation code

<div id="my-wrapper"></div>
<script>
  var myWrapper = document.getElementById('my-wrapper');
</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
Separate Reflows
// first add the list, for testing purposes
myWrapper.innerHTML = '<ul id="my-list"><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul>';


myList = document.getElementById('my-list');

for ( var i = 0, len = myList.childNodes.length; i < len; i++ ) {
    var thisChild = myList.childNodes[i];
   
    if ( thisChild.nodeType == 1 ) {
        thisChild.style.height = '50px';
        thisChild.style.padding = '15px';
    }
}


// remove the list for testing purposes
myWrapper.innerHTML = '';
pending…
Batched Reflows
// first add the list, for testing purposes
myWrapper.innerHTML = '<ul id="my-list"><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul>';


myList = document.getElementById('my-list');

myList.style.display = 'none';

for ( var i = 0, len = myList.childNodes.length; i < len; i++ ) {
    var thisChild = myList.childNodes[i];
   
    if ( thisChild.nodeType == 1 ) {
        thisChild.style.height = '50px';
        thisChild.style.padding = '15px';
    }
}

myList.style.display = 'block';


// remove the list for testing purposes
myWrapper.innerHTML = '';
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