Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listening browser reflow event

I'm trying to listen browsers reflow-events to get an idea what parts of a code are the most expensive ones. Reflow occurs when something must be (re)drawn to screen, for example when new element is added to DOM.

Is there a way to listen thease events for example in/with Javascript, for further analysis?

like image 877
Tohveli Avatar asked May 08 '14 22:05

Tohveli


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's the difference between reflow and repaint?

Repaint differs from reflow as reflow involves changes that affect layout of an element, repaint occurs when changes are made to an elements skin, but do not affect its layout like setting outline, visibility, background or color property of an element.

What is repaint and when does this happen?

A repaint occurs when changes are made to an elements skin that changes visibly, but do not affect its layout. Examples of this include outline , visibility , background , or color .


1 Answers

I think the solution it's to use the DOM MutationObserver class. As the Docs point out:

It is designed as a replacement for Mutation Events defined in the DOM3 Events specification. Api Docs

The example on the site is pretty self explanatory

// select the target node
var target = document.querySelector('#some-id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.type);
  });    
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
observer.disconnect();
like image 107
ABeltramo Avatar answered Nov 08 '22 00:11

ABeltramo