JavaScript How to count reflows in the browser via JavaScript

I want to write an unit test for a JavaScript function and measure how often a function triggered a reflow in the browser. Given the following example:

// Bad example
function foo(elements) {
    for (var i = 0; i < elements.length; i++) {
        elements[i].className = 'w' + elements[i].offsetWidth;
    }
}
// Good example
function foo(elements) {
    var widths = [];
    for (var i = 0; i < elements.length; i++) {
        widths[i] = elements[i].offsetWidth;
    }
    for (i = 0; i < elements.length; i++) {
        elements[i].className = 'w' + widths[i];
    }
}

The unit test should fail for the first example because it triggers a reflow for every element, but should pass for the second example because it doesn’t trigger a reflow.

The only thing that comes near to what I want is mozPaintCount, but it only increments for every paint and not for every reflow. I would need something like mozReflowCount or mozRecalculateStyleCount, but I didn’t find anything likely in any browser. If such a property exists the unit test could look like this:

// mozReflowCount doesn’t exist, such a property is what I’m looking for
var reflowCountBefore = window.mozReflowCount;
foo(document.querySelectorAll('*'));
if (reflowCountBefore === window.mozReflowCount) {
    // passed
}
else {
    // failed
}

I know that I can take a look at the developer tools of the browser to check manually how many reflows or “forced synchronous layouts” were triggered, but I don’t know how to access that data from within a JavaScript unit test.

Q: How do I count reflows in the browser via JavaScript?

Answer:1

var selectorText = "body" ; // first variant // var selectorText = "div#id, div.body"; // multiple selector variant // var selectorText = "div#id ul li, div.body"; // multiple selector variant var C1 =...

var selectorText = "body" ; // first variant // var selectorText = "div#id, div.body"; // multiple selector variant // var selectorText = "div#id ul li, div.body"; // multiple selector variant var C1 =...

When _.debounce is called multiple times, it only apply with the last call's argument. var f = _.debounce(function(a) { console.log(a); }) f(12) f(1223) f(5) f(42) //output -----> 42 Is ...

When _.debounce is called multiple times, it only apply with the last call's argument. var f = _.debounce(function(a) { console.log(a); }) f(12) f(1223) f(5) f(42) //output -----> 42 Is ...

  1. lodash debounce last
  2. lodash debounce cancel previous
  3. lodash debounce
  4. lodash.debounce'

I have this JQuery click event: jq("#generateButton").click({key: "hello world"}, function () { console.dir(this);// this is the "generateButton" html element. // jq(this) would ...

I have this JQuery click event: jq("#generateButton").click({key: "hello world"}, function () { console.dir(this);// this is the "generateButton" html element. // jq(this) would ...

  1. send custom data to google analytics
  2. send custom data to paypal
  3. mpi send custom data type
  4. fcm send custom data
  5. datatable send custom data
  6. ajax send custom data
  7. new relic send custom data
  8. paypal button send custom data
  9. jquery ajax send custom data
  10. push notification send custom data
  11. how to send custom data in wordpress
  12. contact form 7 send custom data

I know this is something usual, With the earlier versions of chrome I used to set "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --args --disable-web-security --user-data-dir in chrome ...

I know this is something usual, With the earlier versions of chrome I used to set "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --args --disable-web-security --user-data-dir in chrome ...

  1. google chrome wildcard certificate issue
  2. google chrome wildcard certificate
  3. google chrome wildcard search
  4. google chrome find wildcard
  5. google chrome pop up blocker wildcard