Long Tasks: When Your Website Gets Stuck on a Single Job

6 min read

What are Long Tasks?

Imagine you're at a grocery store with only one cashier. If someone ahead of you has a massive cart full of items, you have to wait while all those items get scanned before you can check out—even if you only have a few things. That's essentially what a Long Task is in your web browser.

In technical terms, a Long Task is any piece of code that takes more than 50 milliseconds to execute and blocks the browser's main thread. During this time, your website can't respond to user interactions like clicks, scrolls, or typing, creating a sluggish, unresponsive experience.

What defines a Long Task?

  • Not a Long Task: Tasks under 50 milliseconds (User won't notice any delay)
  • Long Task: Any single task that takes 50+ milliseconds (Creates noticeable unresponsiveness)
  • Very Long Task: Tasks over 200 milliseconds (Users will definitely perceive the website as laggy)

Why Long Tasks Make Your Website Feel Broken

Long Tasks significantly impact how users perceive your website's quality and professionalism:

  • Delayed Responses: When users click a button during a Long Task, nothing happens until the task completes, making them wonder if the button is broken.
  • Typing Lag: Form input feels sluggish when Long Tasks interrupt the processing of keystrokes, creating a frustrating typing experience.
  • Choppy Scrolling: Long Tasks cause stuttering and jank when scrolling, giving the impression of a poorly built website.
  • Missed Interactions: If users click multiple times due to no response, their actions might get queued and then all execute at once when the Long Task finishes, causing unexpected behavior.

Research shows that users perceive delays of more than 100 milliseconds as laggy, and anything over 50 milliseconds can affect the feeling of direct manipulation when scrolling or dragging elements. This makes the 50-millisecond threshold for Long Tasks particularly significant.

The Magic Number: Why 50 Milliseconds?

You might wonder why 50 milliseconds specifically is considered the threshold for Long Tasks. There's solid reasoning behind this number:

  • Human Perception: Research in human-computer interaction has shown that delays under 50ms feel "instant" to most users, while longer delays start to feel disconnected from their actions.
  • Frame Budget: To maintain a smooth 60 frames per second (the standard refresh rate of most displays), each frame has a budget of about 16.7ms. A task of 50ms spans multiple frames, causing visible stuttering.
  • Input Responsiveness: For a website to feel responsive, it should respond to user input within 50ms. Longer tasks prevent this quick response.

The Illusion of Performance

What makes Long Tasks particularly troublesome is that they can occur on websites that otherwise appear to load quickly. A site might finish loading in 2 seconds (which is good), but then feel sluggish and unresponsive during use due to Long Tasks that occur during interactions.

Common Causes of Long Tasks

Long Tasks typically come from several common sources:

  • Heavy JavaScript Initialization: When large frameworks or libraries initialize on page load, they often create Long Tasks as they set up their internal state.
  • Complex Calculations: Data processing, sorting large arrays, or running complex algorithms can easily exceed the 50ms threshold.
  • Inefficient DOM Operations: Adding, removing, or manipulating many DOM elements at once forces the browser to recalculate layouts, which can be very time-consuming.
  • Third-Party Scripts: Analytics, advertising, or social media widgets often run lengthy operations when they load or execute.
  • Event Handlers: Code that runs in response to frequent events like scrolling or resizing can create Long Tasks if it's not optimized.

Understanding the common causes helps identify where to look when optimizing your website for better responsiveness.

How to Identify Long Tasks on Your Website

Finding Long Tasks requires specialized tools that can measure JavaScript execution time:

  • Performance monitoring tools: Use specialized tools that can detect and report Long Tasks during real user visits.
  • Browser developer tools: Most modern browsers include performance profiling capabilities that can identify Long Tasks during recordings.
  • Long Tasks API: For more technical users, the browser's built-in Long Tasks API can be used to programmatically detect and measure these issues.

When looking for Long Tasks, pay special attention to:

  • The initial page load (when scripts are being parsed and executed)
  • Moments of user interaction (clicking buttons, submitting forms)
  • Continuous interactions (scrolling, dragging, typing)
  • After loading new content (infinite scrolling, page transitions)

7 Effective Ways to Break Up Long Tasks

1. Implement Time-Slicing

Large operations can be broken into smaller pieces that execute over time, allowing the browser to handle user interactions in between.

Simple fix: Use setTimeout() with zero or small delays to break up work into smaller chunks. For example, if processing 1000 items causes a Long Task, process 100 items at a time with a small delay between batches.

2. Use RequestAnimationFrame for Visual Updates

When modifying the DOM or making visual changes, requestAnimationFrame helps sync these with the browser's render cycle.

Simple fix: Instead of making multiple visual updates in a single long script, use requestAnimationFrame to schedule updates to occur during the browser's natural render cycle.

3. Adopt Asynchronous Operations

Not everything needs to happen immediately and synchronously; many operations can happen asynchronously without blocking the main thread.

Simple fix: Use Promise-based code and async/await syntax to make operations non-blocking. This allows other tasks to execute while waiting for results.

4. Move Work to Web Workers

Web Workers allow JavaScript to run in a background thread, keeping the main thread free for user interactions.

Simple fix: Move data processing, calculations, and other non-UI work to Web Workers. While there's some complexity in setting them up, the responsiveness benefits are significant for computation-heavy websites.

5. Implement Idle-Time Processing

Not all tasks need to happen immediately; some can wait until the browser is less busy.

Simple fix: Use requestIdleCallback() to schedule non-urgent tasks during browser "idle" periods when the user isn't actively interacting with the page.

6. Apply Debouncing and Throttling

Event-driven tasks (like scroll handlers or input processing) can fire rapidly and create many Long Tasks.

Simple fix: Debounce functions that don't need to run on every event (run once after events stop), or throttle functions that should run periodically during continuous events (run at most once every X milliseconds).

7. Prioritize Critical User Interactions

Not all user interactions are equally important; some need immediate responses while others can wait.

Simple fix: Ensure code that handles direct user interactions (like clicking a button) is lean and fast, while deferring less critical operations (like logging or analytics) to run afterwards.

Common Long Tasks Issues and Solutions

Problem: Heavy Initial Load

What's happening: Large JavaScript bundles are causing Long Tasks during the initial page load as they parse and execute.

Simple solution: Implement code splitting to load only essential JavaScript first, then load other features on demand. Use modern build tools to break your code into smaller chunks.

Problem: Slow Search or Filter Functionality

What's happening: Searching or filtering through large datasets is creating Long Tasks when users type or select filters.

Simple solution: Implement debouncing on search inputs to prevent filtering on every keystroke, and consider moving filter logic to a Web Worker for larger datasets.

Problem: Janky Infinite Scrolling

What's happening: Adding new content during infinite scrolling is causing Long Tasks that make scrolling stutter.

Simple solution: Pre-fetch content before it's needed, prepare DOM elements off-screen, and add new content in smaller batches rather than all at once.

Problem: Sluggish Form Validation

What's happening: Complex form validation logic is creating Long Tasks that make typing in forms feel laggy.

Simple solution: Implement progressive validation that checks fields when users finish typing rather than on every keystroke, and optimize validation code to be as efficient as possible.

Long Tasks vs. Other Performance Issues

Understanding how Long Tasks differ from other performance issues helps focus your optimization efforts:

Issue TypeWhat It AffectsHow It Differs from Long Tasks
Slow Page LoadInitial loading experienceAffects the start of the user experience, while Long Tasks can occur at any time, even on "fast" loading pages
Network LatencyTime to receive contentNetwork issues delay content delivery, while Long Tasks delay processing even after content has loaded
Memory LeaksLong-term performanceMemory issues gradually degrade performance over time, while Long Tasks cause immediate, noticeable pauses
Resource SizeDownload timeLarge resources take longer to download but might process quickly, while small but complex code can cause Long Tasks

Long Tasks are unique because they directly impact the moment-by-moment interactivity and responsiveness, regardless of download speeds or server performance.

Real-World Benefits of Fixing Long Tasks

Companies that have prioritized breaking up Long Tasks have seen substantial improvements in user experience and business metrics:

  • E-commerce platform broke up Long Tasks in their product filtering system by moving filtering logic to a Web Worker. This reduced interaction delays by 80% and increased product views by 24% as users could browse more smoothly.
  • Travel booking site implemented time-slicing for their search results processing, reducing maximum task time from 300ms to under 50ms. This improved search completion rates by 17% and increased bookings by 9%.
  • News website optimized their infinite scrolling implementation to avoid Long Tasks, resulting in 35% more articles read per session as users experienced smoother scrolling.
  • Online form application refactored their validation logic to run asynchronously, reducing input lag by 70%. This led to a 23% increase in form completions and fewer support requests about "laggy" forms.

These examples show that addressing Long Tasks isn't just about technical performance metrics—it directly impacts how users interact with your website and whether they accomplish what they came to do.

Conclusion: Breaking Down the Work for a Smoother Experience

Think of Long Tasks like carrying all your grocery bags from the car in one trip instead of making several smaller trips. While it might seem more efficient to do everything at once, it actually makes the process more difficult and risky—you might drop something or strain yourself.

In the same way, breaking up JavaScript work into smaller pieces creates a smoother, more reliable experience, even if the total processing time remains the same. Users perceive a website that responds quickly to their actions as faster and more professional, even if the actual work takes just as long to complete in total.

The key insight about Long Tasks is that they're often unnecessary—most lengthy operations can be broken up, deferred, or moved off the main thread. This isn't just about making your website technically faster; it's about creating an experience that feels responsive and trustworthy to your visitors.

By identifying and breaking up Long Tasks, you're ensuring your website remains responsive throughout the entire user journey, from initial load through every click, scroll, and interaction. This creates a professional experience that builds trust and keeps users engaged with your content.

Ready to make your website feel more responsive?

Greadme's easy-to-use tools can help you identify Long Tasks on your website and provide simple, step-by-step instructions to break them into smaller, more manageable pieces—even if you're not technically minded.

Create a Smoother User Experience Today