puppeteer: [Bug]: launch({headless: "new"}) is significantly slower than launch({headless: "old"}), especially for Page.pdf

Bug expectation

I expected the new headless mode to be roughly on par (performance-wise) with the old one, but I am seeing 10x+ worse performance with the new mode. Perhaps my ticket is misplaced though. I am guessing it has to do with the consolidation mentioned in https://developer.chrome.com/articles/new-headless/. Please close if that’s the case.

Here’s the output of my example script, which generates 10 very basic pdfs in old and then new modes.

Finished generating puzzles in mode ‘old’ (2.9s) Finished generating puzzles in mode ‘new’ (36.7s)

Bug behavior

  • Flaky
  • PDF

Minimal, reproducible example

import { performance } from "perf_hooks";
import process from "process";
import fs from "fs";

import { launch } from "puppeteer";

const workingDir = `${process.cwd()}/files`;

async function printToPdf(mode) {
  for (const i of [...Array(10).keys()]) {
    const browser = await launch({headless: mode});
    const page = await browser.newPage();
    await page.goto(`${workingDir}/file.html`);
    await page.pdf({path: `${workingDir}/file${i}.pdf`});
    await browser.close();
  }
}

async function measurePerf(mode) {
  const start = performance.now();
  await printToPdf(mode);
  const end = performance.now();
  console.log(`Finished generating puzzles in mode '${mode}' (${((end - start) / 1000).toFixed(1)}s)`);
}

if (!fs.existsSync(workingDir)){
  fs.mkdirSync(workingDir);
}

const writeStream = fs.createWriteStream(`${workingDir}/file.html`);
writeStream.write("<html><body>Testing</body></html>");
writeStream.end();

measurePerf("old");
measurePerf("new");

Error string

Just a performance regression; there’s no error string

Puppeteer configuration

No response

Puppeteer version

19.11.0

Node version

18.16.0

Package manager

npm

Package manager version

9.5.1

Operating system

Windows

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 12
  • Comments: 16

Most upvoted comments

we expect the new headless to be perhaps up to 30% slower

😱 this doesn’t sound like a positive development from a user perspective?

@ramicohen303 I don’t think so. But we will change to using the new-headless by default in Puppeteer at some point and the old mode will become optional.

@drmrbrewer the goal is to minimize the feature gap between headful and the headless (e.g., headless has no extensions, special iframe treatment and several other changes etc). Startup performance is on the second place and I expect it to be improved with the next releases of Chromium (i.e., it’s not smth set in stone forever.) Note that you can still use the old headless mode even if Puppeteer will start using the new one by default (which will not happen this year most likely). But thanks everyone for the feedback and trying it out on your use cases.

P.S. I have passed on the feedback to the team working on the new headless.

Interesting, in general, we expect the new headless to be perhaps up to 30% slower and, in general, be the same compared with the headful browser. What numbers do you get for the headful browser?

There are no specific updates. I believe performance has improved in some areas but, in general, if performance is the same as in the headful mode, it’s acceptable for the new headless. I think the best would be to close the issue here as Puppeteer cannot really improve the performance of Chrome directly. Let’s file crbugs (via crbug.com) requesting specific areas of improvements but note that from the browser perspective the launch and close performance are not a priority. If it’s important for you, you can still use the old headless mode (which is technically a different browser implementation, see https://developer.chrome.com/articles/new-headless/)

Observing very long time spent in browser.close() as well, even when not PDF involved, unfortunately do not have a reproducible test case at this time.

I’m seeing incredibly higher times comparing headless: true to headless: "new", on Windows 10 node, as well as a considerable extra time for node to exit after await browser.close() has completed Tests using normal node or cmd resulted in millisecond differences

Old headless (Node closes as soon as main() is completed);

PS > ts-node .\test.ts
default: 0.057ms
  {Puppeteer old Headless deprecation warning}
default: 1.616s
default: 1.616s
PDF generated successfully!

New headless (Node takes ~15 seconds to exit after main() completes):

PS > ts-node .\test.ts
default: 0.052ms
default: 4.073s
PDF generated successfully!

PS > Measure-Command { ts-node .\test.ts }
Seconds           : 20
Milliseconds      : 228
Ticks             : 202282756

Code:

var fs = require("fs");
var puppeteer = require("puppeteer");

async function generatePDF(html, outputPath) {
	const browser = await puppeteer.launch({ headless: "new" });
	const page = await browser.newPage();

	await page.setContent(html);
	await page.pdf({ path: outputPath });

	await browser.close();
}

function readHTMLFile(filePath) {
	return new Promise((resolve, reject) => {
		fs.readFile(filePath, 'utf8', (error, data) => {
			if (error) {
				reject(error);
			} else {
				resolve(data);
			}
		});
	});
}


async function main() {
	try {
		const htmlFilePath = './input.html';
		const content = await readHTMLFile(htmlFilePath);

		console.time();
		console.timeLog();
		await generatePDF(content, 'output.pdf');
		console.timeEnd();

		console.log('PDF generated successfully!');
	} catch (error) {
		console.error('Error generating PDF:', error);
	}
}

main();

Using puppeteer 20.1.2

It appears most of the time in the new headless codepath is spent either generating the pdf or saving it to disk. I arrived at this conclusion from the following:

I re-ran the script (as provided in the description): Finished generating puzzles in mode ‘old’ (4.6s) Finished generating puzzles in mode ‘new’ (40.3s)

Then reran with the page.pdf call disabled/commented out: Finished generating puzzles in mode ‘old’ (4.6s) Finished generating puzzles in mode ‘new’ (7.1s)

This report is about the launch, I expect some differences in other operations but they should be less significant or the same.