How to measure TTFB with Puppeteer?

Viewed 70

Is it possible to calculate the TTFB with Puppeteer?

I couldn't find anything in their docs.

I currently have this code:

const browser = await puppeteer.launch(launchOptions);
const page = await browser.newPage();
const response = await page.goto(url);
const { status } = response;
3 Answers

This might do what you want:

let start = new Date()
page.once('response', () => console.log(new Date() - start))
await page.goto(url)

This is how I solved it:

const browser = await puppeteer.launch(launchOptions);
const page = await browser.newPage();
await page.goto(url);
const navigationTimingJson = await page.evaluate(() =>
  JSON.stringify(performance.getEntriesByType("navigation"))
);
const [navigationTiming] = JSON.parse(navigationTimingJson)
const TTFB = navigationTiming.responseStart - navigationTiming.requestStart;
Related