A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

API for save layout read operation (efficiently preventing layout thrashing)

aFarkas
2016-03-23

I was send from a rIC issue.

Currently it is impossible to know whether the layout was invalidated and a read operation would force a layout.

While it is possible to write your own code in a way to fully avoid layout thrashing. In the real world this becomes quite impossible as soon as third party libraries and services are included in a page.

The currently nearest API that gets in the requested direction is the requestIdleCallback API. As seen above this API can’t guarantee that the callback is invoked, when the layout does not need recalculations.

I would like to see either a method/getter to check whether layout is currently dirty:

window.isSaveToRead();

Or maybe better a callback method similar to rIC or rAF:

window.requestReadCallback(()=>{
    //do some read operations
});

This callback function could be invoked in the same frame if layout is currently not invalidated or if it is, will be invoked right after frame commit before any other tasks (callbacks and event listeners) are run.