Measure Cumulative Layout Shift in the console
- Performance
Cumulative Layout Shift (CLS) measures visual instability — how much the page content jumps around during loading. A poor CLS score means elements are moving after the user has started reading or interacting. This snippet monitors shifts in real time.
/**
* Measure Cumulative Layout Shift (CLS)
*
* Monitors layout shifts in real time using the PerformanceObserver API
* and reports the cumulative CLS score. Logs each individual shift with
* its value and the elements involved, then shows a running total.
* Useful for debugging layout instability during development.
*
* Usage: Run in browser console on the page you want to measure.
* Scroll the page and interact with it to trigger shifts.
*
* @category performance
* @language javascript
*
* @author Adame Dahmani https://adame.io
* @support Support my family project: https://adame.io/fundraiser
*/
(function () {
var totalCLS = 0;
var shifts = [];
new PerformanceObserver(function (list) {
list.getEntries().forEach(function (entry) {
if (!entry.hadRecentInput) {
totalCLS += entry.value;
shifts.push({
value: entry.value.toFixed(4),
sources: entry.sources
? entry.sources.map(function (s) {
return s.node
? s.node.nodeName + (s.node.className ? '.' + s.node.className.split(' ')[0] : '')
: 'unknown';
})
: [],
});
console.log(
'Layout shift: ' + entry.value.toFixed(4) +
' | Total CLS: ' + totalCLS.toFixed(4)
);
}
});
}).observe({ type: 'layout-shift', buffered: true });
window.getCLS = function () {
console.table(shifts);
console.log('Total CLS: ' + totalCLS.toFixed(4));
return totalCLS;
};
console.log('CLS observer active. Call getCLS() to see results.');
})();Run it in the browser console, then scroll and interact with the page. Each layout shift is logged with its value and the elements involved. Call getCLS() at any time to see a summary table and the running total. A CLS under 0.1 is considered good.