Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in Javascript to time a browser reflow?

I need to be able to benchmark a particular build of a webkit-based browser and am measuring the length of time it takes to do certain stuff like DOM manipulation, memory limits etc.

I have a test below which records the length of time it takes to simultaneously load in 10 fairly heavy PNG graphics. In code, I need to be able to time how long it takes for the load to finish. I have tried setting the onLoad function on the dynamic Image object to produce a time in ms. However, as shown in the cap below it is giving an inaccurate reading because the reading it gives is a tiny amount due to it only recording the data transfer part of the load and then there is a considerable (3000+ms) delay for when the images are viewable - looped in blue, this is the browser reflow cycle.

Is there some event in webkit I can use to record when the browser has finished a reflow so that I can benchmark this? I have to be able to record the time in milliseconds in code because the build of webkit I am testing has no developer tools. I am able to observe the difference in Chrome ok but the performance between the two builds differs drastically and I need to be able to quantify it accurately for comparison.

image benchmark

like image 612
giles Avatar asked May 28 '12 15:05

giles


People also ask

What triggers browser reflow?

Resizing the browser window, using JavaScript methods involving computed styles, adding or removing elements from the DOM, and changing an element's classes are a few of the things that can trigger reflow.

What is reflow in JavaScript?

Reflow means re-calculating the positions and geometries of elements in the document, for the purpose of re-rendering part or all of the document.

What is forced reflow while executing JavaScript?

Force reflow (or Layout Reflow) is a major performance bottleneck. It happens when a measurement of the DOM happens after a DOM mutation. With this knowledge, I was able to improve performance of an app in my workplace by 75%.

How do I minimize my browser reflow?

Tips for minimizing the reflow time The whole recalculating process starts again, and so on. Clean CSS sheet, and remove dead CSS code. Avoid using too much CSS selectors, which require more CPU power to do selector matching. Absolute and fixed positions are reflow friendly.


2 Answers

If you are using jQuery, you could try recording the time between document ready and window load, that would give you an approximation.

(function(){
    var start, end;

    $(document).ready(function(){
         start = new Date()    
    });
    $(window).load(function(){
         end = new Date();
         console.log(end.getTime() - start.getTime());
    });

}());

Edit:

Have you taken a look at the Browserscope reflow timer? Basically it checks to see how long it takes for the browser to return control to the JavaScript engine after changes to the dom. According the the page it should work in any browser, although I haven't tested it personally. Perhaps you could adapt the code run during the tests to time the reflow in your page.

like image 186
Ryan Lynch Avatar answered Oct 04 '22 02:10

Ryan Lynch


Also, you might want to have a look at CSS Stress Test. The bookmarklet is really great for page performance testing. http://andy.edinborough.org/CSS-Stress-Testing-and-Performance-Profiling

How about setting the PNG as a div background-image and running the stress test, it should enable/disable the image multiple times with timings.

like image 21
Paul Avatar answered Oct 04 '22 04:10

Paul