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
<divid="my-wrapper"> Test DIV </div> <script> var myWrapper = document.getElementById('my-wrapper'); </script>
Preparation code output
Test DIV
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='';
0 comments