Latenode

Configuring Headless Mode in Puppeteer: Balancing Speed and Functionality

Explore the differences between headless and headful modes in Puppeteer to optimize automation, debugging, and performance for your projects.

RaianRaian
Configuring Headless Mode in Puppeteer: Balancing Speed and Functionality

Puppeteer lets you automate Chrome or Chromium browsers, and choosing between headless or headful mode can significantly impact performance and debugging. Here's a quick breakdown:

  • Headless Mode: Faster, uses fewer resources, ideal for automation and production tasks like web scraping or testing.
  • Headful Mode: Visual browser interface, better for debugging and development.

Quick Comparison

FeatureHeadless ModeHeadful Mode
Visual InterfaceNo GUIFull GUI
Resource UsageLowerHigher
Execution SpeedFasterStandard
Memory FootprintSmallerLarger
Debugging EaseConsole-basedVisual feedback

New Headless Mode combines the performance of headless with improved debugging tools, making it a balanced choice for many workflows.

Choose:

  • Standard Headless for efficiency in CI/CD or production.
  • New Headless for advanced debugging and modern features.
  • Headful Mode for visual debugging and testing interactive elements.

This guide explains how to configure each mode, their benefits, and when to use them.

Puppeteer - Headless and Headful Modes

1. Standard Headless Mode

Standard headless mode is Puppeteer's default setup, running Chrome or Chromium without its graphical user interface (GUI). By skipping the GUI, it uses less memory, processes pages faster, and reduces CPU usage.

Key Features

Standard headless mode supports a wide range of browser operations, including:

FeatureWhat It Does
User SimulationHandles clicks, form inputs, navigation
Content ManipulationInjects JavaScript, modifies the DOM
Asset GenerationCaptures screenshots, creates PDFs
Network MonitoringTracks requests and responses
Page AutomationRuns scripts, extracts content

How to Use It

You can launch headless mode with the following code:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>(); <span class="hljs-comment">// Default headless</span>
<span class="hljs-comment">// OR</span>
<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({ <span class="hljs-attr">headless</span>: <span class="hljs-literal">true</span> }); <span class="hljs-comment">// Explicit headless</span>

Why It’s Useful

Headless mode is ideal for tasks like web scraping, automated testing, and creating screenshots or PDFs. For example, Optimizely cut its testing cycle from 24 hours to just one hour by leveraging headless browser testing in the cloud [1].

Challenges to Keep in Mind

While it's efficient, headless mode comes with some hurdles:

  • Certain websites can detect and block headless browsers.
  • Debugging is harder without visual feedback.
  • More complex user interactions may require extra setup.

To improve reliability and avoid detection, you can:

  • Set a custom user-agent string.
  • Add delays between automated actions.
  • Use strong error-handling mechanisms.
  • Ensure the page is fully loaded before extracting data.

For even greater efficiency in specific server-side tasks, you might consider the chrome-headless-shell variant. This lightweight option is perfect for automation-focused workflows. Launch it with:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({ <span class="hljs-attr">headless</span>: <span class="hljs-string">&#x27;shell&#x27;</span> });

This mode is especially effective for server-side operations, offering streamlined performance.

Next, we’ll dive into Chrome’s new headless mode and how it takes performance to the next level.

2. Chrome's New Headless Mode

Chrome has introduced an upgraded headless mode that brings browser automation to a new level. Unlike the older version, this mode uses the same codebase as the regular Chrome browser, ensuring better stability and access to all browser features. It combines the efficiency of headless operation with the functionality of a full browser, making automation and debugging much smoother.

Key Features

The new headless mode operates by creating platform windows without displaying them. This allows you to use the browser's full capabilities while still enjoying the performance benefits of headless operation. Here's how you can use it in your code:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({ <span class="hljs-attr">headless</span>: <span class="hljs-string">&#x27;new&#x27;</span> }); <span class="hljs-comment">// Explicitly use new headless mode</span>
<span class="hljs-comment">// OR</span>
<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({ <span class="hljs-attr">headless</span>: <span class="hljs-literal">true</span> }); <span class="hljs-comment">// Defaults to new headless mode</span>

Debugging Tools

This mode also includes several tools to make debugging easier:

FeaturePurpose
Visual InspectionView browser behavior (set headless: false)
Operation TimingUse the slowMo option to delay actions
Console MonitoringCapture console output for analysis
Protocol AnalysisEnable DevTools protocol logging
Browser DiagnosticsUse dumpio: true for detailed logs

Advanced Troubleshooting

1. Using DevTools for Debugging

You can enable Chrome's built-in DevTools to inspect browser behavior in detail:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
  <span class="hljs-attr">headless</span>: <span class="hljs-string">&#x27;new&#x27;</span>,
  <span class="hljs-attr">devtools</span>: <span class="hljs-literal">true</span>
});

2. Performance Monitoring and Error Tracking

The shared codebase allows for precise performance profiling and error resolution. Use this setup to monitor and debug both client-side and server-side operations:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
  <span class="hljs-attr">headless</span>: <span class="hljs-string">&#x27;new&#x27;</span>,
  <span class="hljs-attr">devtools</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">slowMo</span>: <span class="hljs-number">100</span>,
  <span class="hljs-attr">args</span>: [<span class="hljs-string">&#x27;--enable-logging&#x27;</span>, <span class="hljs-string">&#x27;--v=1&#x27;</span>]
});

Migration Guidance

Although the older headless mode (headless: 'old') is still available, it’s a good idea to switch to the new version. The legacy mode will eventually be phased out, so transitioning now ensures consistency and prepares you for future updates.

Performance Benefits

By using a unified codebase, the new headless mode delivers consistent behavior across various environments. This consistency reduces environment-specific issues and enhances the reliability of automated workflows.

sbb-itb-23997f1

3. Browser Display Mode

Browser display mode opens a visible browser window during Puppeteer operations. While it uses more resources, it offers better debugging tools and visual feedback.

Performance Considerations

Running in display mode increases resource usage but provides some advantages:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
  <span class="hljs-attr">headless</span>: <span class="hljs-literal">false</span>,
  <span class="hljs-attr">args</span>: [<span class="hljs-string">&#x27;--enable-gpu-rasterization&#x27;</span>],
  <span class="hljs-attr">defaultViewport</span>: <span class="hljs-literal">null</span>
});

With a visible browser, GPU hardware acceleration can be enabled, which improves performance on image-heavy websites. Additionally, this mode reduces the chances of bot detection by mimicking standard browser behavior.

Enhanced Debugging Features

Display mode is ideal for troubleshooting, offering immediate visual feedback. Here's a useful debugging setup:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
  <span class="hljs-attr">headless</span>: <span class="hljs-literal">false</span>,
  <span class="hljs-attr">devtools</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">slowMo</span>: <span class="hljs-number">250</span>,
  <span class="hljs-attr">defaultViewport</span>: {
    <span class="hljs-attr">width</span>: <span class="hljs-number">1920</span>,
    <span class="hljs-attr">height</span>: <span class="hljs-number">1080</span>
  }
});

This configuration opens DevTools automatically, introduces a 250ms delay for better inspection, and uses a standard viewport size for consistent testing.

Usage Scenarios

ScenarioBenefitsConfiguration Tips
Visual DebuggingSee interactions in real-timeEnable DevTools, use slowMo
GUI TestingObserve user interface behaviorsSet a fixed viewport size
Complex WorkflowsVerify detailed interactionsCombine with console logging

Optimization Tips

To keep things running smoothly in display mode:

  • Manage Resources: Close unused tabs and windows to save memory.
  • Monitor Performance: Enable browser logging to track performance metrics.
  • Use GPU Acceleration: Activate GPU features for tasks involving heavy graphics.

"Sometimes it's useful to see what the browser is displaying. Instead of launching in headless mode, launch a full version of the browser with headless set to false." - Puppeteer Documentation [2]

Advanced Configuration

For more demanding tasks, you can enhance display mode with additional settings:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
  <span class="hljs-attr">headless</span>: <span class="hljs-literal">false</span>,
  <span class="hljs-attr">args</span>: [
    <span class="hljs-string">&#x27;--enable-gpu-rasterization&#x27;</span>,
    <span class="hljs-string">&#x27;--window-size=1920,1080&#x27;</span>,
    <span class="hljs-string">&#x27;--disable-web-security&#x27;</span>
  ],
  <span class="hljs-attr">dumpio</span>: <span class="hljs-literal">true</span>
});

This setup improves visibility, enables detailed logging, and allows cross-origin access. These tweaks ensure a smoother experience and lay the groundwork for comparing performance and functionality in the next guide.

Mode Comparison Guide

When setting up Puppeteer, each mode offers a different mix of speed, resource usage, and debugging capabilities. Picking the right one depends on your specific needs.

Here’s a breakdown of the modes:

  • Standard Headless Mode
    This mode is built for efficiency and quick startup, making it perfect for automated testing or CI/CD pipelines. It uses minimal system resources and provides basic debugging output via the console.
  • New Headless Mode
    Combines strong debugging features with performance close to Standard Headless Mode. It’s a good choice for tasks that require a mix of speed and functionality.
  • Browser Display Mode
    This mode gives you full access to Chrome DevTools, offering real-time visual feedback. It’s ideal for detailed debugging or analyzing complex workflows. While it demands more system resources, it supports tasks that benefit from hardware acceleration and advanced network controls.

Puppeteer leverages the Chrome DevTools Protocol for precise browser control, including network interception and JavaScript execution, allowing you to fine-tune configurations.

For instance, here’s how you can optimize the New Headless Mode:

<span class="hljs-keyword">const</span> browser = <span class="hljs-keyword">await</span> puppeteer.<span class="hljs-title function_">launch</span>({
  <span class="hljs-attr">headless</span>: <span class="hljs-string">&#x27;new&#x27;</span>,
  <span class="hljs-attr">args</span>: [
   <span class="hljs-string">&#x27;--disable-gpu&#x27;</span>,
   <span class="hljs-string">&#x27;--no-sandbox&#x27;</span>,
   <span class="hljs-string">&#x27;--disable-setuid-sandbox&#x27;</span>
  ]
});

Use Standard Headless for fast, resource-efficient automation, Browser Display for detailed debugging, or New Headless when you need a balance of performance and functionality.

Choosing the Right Mode

When selecting a mode in Puppeteer, consider your project's specific goals and requirements. Puppeteer emphasizes speed, security, stability, and simplicity [3]. Here's a breakdown to help you decide:

Standard Headless Mode is ideal if you need:

  • Maximum performance while using minimal resources
  • No visual debugging or browser rendering
  • A reliable option for CI/CD environments

New Headless Mode works best when you require:

  • Advanced debugging tools without sacrificing performance
  • Compatibility with modern web features
  • A balance between resource use and functionality
  • Access to most Chrome DevTools Protocol features

Browser Display Mode is suited for scenarios that involve:

  • Detailed debugging needs
  • Visual confirmation of automation workflows
  • Development and testing of interactive elements
  • The use of hardware acceleration

Common Use Cases and Recommendations:

Project TypeRecommended ModeKey Advantage
CI/CD Pipeline TestingStandard HeadlessFast execution with minimal resource usage
Development & DebuggingBrowser DisplayComplete visual feedback and DevTools
Production AutomationNew HeadlessA solid mix of features and performance

Puppeteer’s API makes it easy to switch between these modes as your project evolves.

Related posts

Raian

Researcher, Nocode Expert

Author details →