Skip to main content
XenonStack

Data Visualization

XenonStack Blog Minimise Main Thread Work: Practical Guide

| Published | Updated

Minimise Main Thread Work: Practical Guide
7:15

Key takeaway

  • Main thread work delays page rendering and input response. Learn what causes it, the Lighthouse 4 second threshold and the fixes that cut it down.

Main thread work is the time the browser's main thread spends parsing HTML and CSS, building the DOM, evaluating JavaScript and handling user input, and minimising it keeps a page responsive while it loads. Because the main thread does these jobs one task at a time, anything that keeps it busy delays what the visitor sees and what happens when they tap or click.

This guide is for front-end developers, web performance owners and product teams who want a clear picture of what loads the main thread, what thresholds to watch and which fixes come first. It also links to our guides on code splitting, optimising layout shifting and user experience design.

What is the main thread and what does it do?

The Chrome team's Lighthouse documentation on main thread work explains that the renderer's main thread handles most of the work that turns code into a page: parsing HTML and building the DOM, parsing CSS and applying styles, and parsing, evaluating and running JavaScript. It also processes user events. When the thread is busy with other work, the page may not respond to interactions.

That is why a heavy page can look finished and still feel frozen. The pixels may be on screen, but the thread that would answer a tap is occupied.

How much main thread work is too much?

Two published thresholds are worth knowing. Lighthouse flags pages that keep the main thread busy for longer than 4 seconds during load. Separately, the web.dev article on optimising long tasks defines a long task as one that runs for more than 50 milliseconds, and says long tasks contribute to poor Interaction to Next Paint (INP).

MeasureWhat it tells youThreshold stated by the source
Main thread busy time during loadTotal time the thread is occupied while the page loadsFlagged above 4 seconds (Lighthouse)
Long taskA single task that blocks input and renderingLonger than 50 ms (web.dev)
Interaction to Next PaintHow quickly the page responds visually after inputAffected by long tasks (web.dev)

What causes heavy main thread work?

Lighthouse breaks the load into categories, and each one points to a different fix. The table follows the categories and recommendations on that page.

Two-column diagram. Sources of main thread work: script evaluation, parsing and compiling, style and layout, HTML and CSS parsing. Recommended fixes: workers and debounce, split and remove, simplify layout, critical CSS.
CategoryTypical causeRecommended direction
Script evaluationLarge or third-party JavaScript running at loadOptimise third-party code, debounce input handlers, move work to web workers
Script parsing and compilationBig bundles and code that is never usedSplit code into smaller payloads, remove unused code
Style and layoutComplex style calculations and layout thrashingReduce scope and complexity, avoid large, complex layouts
RenderingMany layers and large paint areasUse compositor-only properties, limit layers, reduce paint complexity
Parsing HTML and CSSLarge render-blocking stylesheetsExtract critical CSS, minify CSS, defer non-critical CSS

How do you reduce main thread work?

Start by measuring, then remove, then defer, then move. In Chrome DevTools you can see exactly what the main thread does during load, and the Lighthouse breakdown shows where the time goes.

  • Remove what you do not need. Delete unused code and scripts that no longer add value. Smaller payloads mean less to parse, compile and run.
  • Split what remains. Send only the code needed for the first view and load the rest later. Our guide to code splitting shows one approach.
  • Control third-party code. Review each third-party script, drop any that does not earn its place, and delay the non-essential ones.
  • Break up long tasks. The web.dev article describes yielding to the main thread so the browser can handle input and rendering first. scheduler.yield() pauses work and resumes it with priority, but the article lists support as Chrome and Edge 129 and later and Firefox 142 and later, not Safari, so use a setTimeout-based fallback or a polyfill.
  • Move work off the thread. Web workers run JavaScript in parallel with the main thread, which suits heavy computation that does not touch the page directly.
  • Trim CSS. Minify stylesheets, extract the critical CSS the first view needs and defer the rest. Minification removes characters such as comments and whitespace without changing how the page looks.

One caution on dates: the Lighthouse page was last updated in 2019 and the thresholds and categories above come from it, so confirm them in the current documentation before you set a team target. The web.dev article no longer recommends isInputPending(), because it can return false negatives and does not cover animations or other UI updates.

How do AI agents change main thread optimisation?

Performance work is repetitive: run an audit, read the breakdown, find the script or stylesheet at fault, test a fix, repeat. AI agents can take on the routine part of that loop, such as running audits on every release, comparing the breakdown with the last build and drafting a ticket for the script that grew. People still decide what to remove, because that depends on what the business needs the page to do. Teams that want to build and run agents for testing and automation workflows can look at ElixirClaw, the AI agents computing platform. Agent output should be checked against the same documented thresholds before anything ships.

What should you do first?

  1. Run a Lighthouse audit and open the main thread breakdown for your slowest template.
  2. Find the largest script evaluation and parsing costs, and remove unused code first.
  3. Review every third-party script and delay or remove the ones you cannot justify.
  4. Break up tasks longer than 50 ms in the interactions your visitors use most.
  5. Add the audit to your release checks so new scripts are caught early. For wider design choices, see our principles of user interface design, our digital product development services and our guide to secure coding best practices, since third-party scripts are also a security decision.

To map one decision your agents make, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.

Frequently asked questions

What is main thread work?

It is the work the browser's main thread does to build and run a page: parsing HTML and CSS, building the DOM, evaluating JavaScript and processing user events.

What counts as a long task?

The web.dev article defines a long task as any task that runs for more than 50 milliseconds. The part beyond 50 ms is its blocking period.

Do web workers reduce main thread work?

Yes. Web workers move JavaScript off the main thread, so heavy computation no longer competes with rendering and input.

Does minifying CSS help the main thread?

Lighthouse lists minifying CSS, extracting critical CSS and deferring non-critical CSS as ways to reduce the cost of parsing HTML and CSS.

Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.

Ready to put AI agents to work? Explore ElixirClaw, the agentic OS platform to build, orchestrate and govern AI agents.

Table of Contents

navdeep-singh-gill

Navdeep Singh Gill

Global CEO and Founder of XenonStack

LinkedIn profile

Navdeep Singh Gill is serving as Chief Executive Officer and Product Architect at XenonStack. He holds expertise in building SaaS Platform for Decentralised Big Data management and Governance, AI Marketplace for Operationalising and Scaling. His incredible experience in AI Technologies and Big Data Engineering thrills him to write about different use cases and its approach to solutions.

Get the latest articles in your inbox

Subscribe Now

Interested in solving your challenges with the XenonStack team?
Get Started

Tell us about you and your priorities so we can prepare the right conversation. It takes about 2 minutes.

Please Select your Industry
Banking
Fintech
Payment Providers
Wealth Management
Discrete Manufacturing
Semiconductor
Machinery Manufacturing / Automation
Appliances / Electrical / Electronics
Elevator Manufacturing
Defense & Space Manufacturing
Computers & Electronics / Industrial Machinery
Motor Vehicle Manufacturing
Food and Beverages
Distillery & Wines
Beverages
Shipping
Logistics
Mobility (EV / Public Transport)
Energy & Utilities
Hospitality
Digital Gaming Platforms
SportsTech with AI
Public Safety - Explosives
Public Safety - Firefighting
Public Safety - Surveillance
Public Safety - Others
Media Platforms
City Operations
Airlines & Aviation
Defense Warfare & Drones
Robotics Engineering
Drones Manufacturing
AI Labs for Colleges
AI MSP / Quantum / AGI Institutes
Retail Apparel and Fashion

Continue