Total Blocking Time (TBT) Optimization Fix
In this tutorial, you'll learn about Total Blocking Time (TBT) Optimization Fix. We cover key concepts, practical examples, and best practices.
The Problem
Total Blocking Time (TBT) measures the sum of all long task blocking durations between First Contentful Paint (FCP) and Time to Interactive (TTI). A high TBT score makes the page feel unresponsive — Lighthouse flags anything above 300ms.
Quick Fix
Step 1: Measure TBT with Lighthouse
# Run Lighthouse from CLI
npx lighthouse https://example.com --view
# Or use Chrome DevTools > Lighthouse tab
# TBT is shown under "Opportunities"
Step 2: Split JavaScript parsing
// Wrong — single bundle parses all at once
import './heavy-module';
import './analytics';
import './chat-widget';
// Right — dynamic imports spread parsing
// Main bundle:
import('./heavy-module').then(mod => {
mod.init();
});
// Defer non-critical modules
if (window.shouldLoadAnalytics) {
import('./analytics');
}
// Load chat widget on user intent
chatButton.addEventListener('click', () => {
import('./chat-widget').then(mod => {
mod.open();
});
});
Step 3: Defer third-party scripts with scheduling
// Wrong — third-party scripts load immediately
const script = document.createElement('script');
script.src = 'https://third-party.com/sdk.js';
document.head.appendChild(script);
// Right — schedule during idle time
if ('requestIdleCallback' in window) {
requestIdleCallback(() => {
const script = document.createElement('script');
script.src = 'https://third-party.com/sdk.js';
script.async = true;
document.body.appendChild(script);
}, { timeout: 3000 });
} else {
// Fallback: load after initial render
window.addEventListener('load', () => {
const script = document.createElement('script');
script.src = 'https://third-party.com/sdk.js';
document.body.appendChild(script);
});
}
Step 4: Reduce unused JavaScript
// Wrong — imports entire library
import _ from 'lodash';
const result = _.chunk(array, 2);
// Right — tree-shake or use native alternatives
import chunk from 'lodash/chunk';
// Or use native methods
const chunk = (arr, size) => {
return Array.from({ length: Math.ceil(arr.length / size) }, (_, i) =>
arr.slice(i * size, i * size + size)
);
};
Step 5: Use code splitting
// webpack.config.js
module.exports = {
// Wrong — single bundle
// entry: './src/index.js',
// Right — split by route
entry: {
main: './src/index.js',
dashboard: './src/dashboard.js',
settings: './src/settings.js'
},
optimization: {
splitChunks: {
chunks: 'all',
}
}
};
Prevention
- Monitor TBT in Lighthouse CI to catch regressions
- Use dynamic imports for route-based code splitting
- Defer third-party scripts with requestIdleCallback
- Tree-shake unused code and prefer native APIs over libraries
- Keep main thread tasks under 50ms using task splitting
Common Mistakes with tbt optimization
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world PERF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro