Skip to main content

How to Optimize Puppeteer Automations 2024

· 4 min read
Mark

Puppeteer is a web analysis, data mining, and test automation tool. Puppeteer uses Headless Chrome. The more complex the task, the more time Puppeteer needs to complete it. In this article, we will look at the main reasons for the slowdown in automation and ways to optimize it.

Why does automation speed matter?

It is necessary to identify the factor that slows down the execution speed of automation scripts and eliminate it. This is supported by two main advantages of optimized automation: [1]. On-demand automations provide users with a faster response. [2]. Scheduled automations reduce resource consumption. Developers often face increasing hosting bills due to a lack of automation on their own servers or choosing a slow competitor.

What's slowing down your Puppeteer scripts?

The execution speed of Puppeteer scripts is affected by various factors, including:

  • Network Latency in Loading Resources: There are various resources, like images or CSS and JavaScript files, present on web pages. The more resources a web page contains, the longer it takes to load them. At the same time, the time that the automation script spends on completing the task increases.
  • Proxies: proxy servers help with web scraping and bypassing bot detection mechanisms. However, despite their benefits, they often cause delays in the automation script. For example, the proxy server is too slow or is located far from the target website's server.
  • Headful Chrome: Using Chrome's offline mode (no GUI) usually speeds up the process and saves memory, but may be detected as a bot action and cause incorrect display. It is preferable to use the Headful browser, but it requires more processing power.
  • Geolocation: If you have enabled automation through a proxy or on the server, geolocation may contribute to delays in loading web pages due to the need to transmit information over long distances.

Speeding Up Your Puppeteer Scripts: Practical Solutions

Now that we're familiar with the reasons why automation is slow, let's look at practical solutions to improve your Puppeteer scripts:

  • Reusing Browser Instances: While you are using our keepalive flag, to save time, it is recommended to use the same browser instance for different tasks instead of starting a new one each time.
  • Reusing Cache and Cookies: With Puppeteer you can reuse cache and cookies. That is, resources that have already been opened during previous sessions will load faster. To reuse cache and cookies, you need to point Puppeteer to the user data directory.
  • Going Headless for Text-Based Tasks: Running Headless Chrome is another way to increase productivity. However, this option is only suitable for you if you do not use a graphical interface during the session and do not monitor bot activity.
  • Intercepting the Network to Skip Unnecessary Resources: Puppeteer supports the function of intercepting network requests. Thanks to this, you can block requests to various resources that the web page contains, such as photos or CSS. At the same time, the loading of services is accelerated since capacity is freed up that would otherwise be spent on loading resources.
  • GPU Hardware Acceleration: If you accelerate the GPU while headful mode is running, it will speed up loading web pages with large numbers of images.
  • Server and Proxy Location: To improve efficiency and comply with GDPR, it is recommended to optimize the geolocation of the servers where your scripts are hosted and the proxy servers used to access the target website.

Conclusion

By identifying the slowdown factor in Puppeteer scripts and using a strategy to eliminate it, you can achieve fast and smooth headless Chrome automation. The speed of task completion is the most important factor, so we pay special attention to it when setting up BrowserCloud. If you have problems with slow automation, you can create a trial account and see the effectiveness of our platform.