Listening browser reflow event

后端 未结 1 1292
独厮守ぢ
独厮守ぢ 2020-12-30 07:14

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 e

相关标签:
1条回答
  • 2020-12-30 08:16

    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();
    
    0 讨论(0)
提交回复
热议问题