How to benchmark page loading times with Puppeteer?

To benchmark page loading times with Puppeteer, you can follow these steps:

  1. Install Puppeteer by running the following command in your terminal:
npm install puppeteer
  1. Create a new javascript file (e.g. benchmark.js) and require Puppeteer at the beginning of the file:
const puppeteer = require('puppeteer');
  1. Write a function that uses Puppeteer to measure the page loading time. This function will open a new browser, navigate to the specified URL, measure the time taken for the page to load completely, and print out the loading time in milliseconds:
async function benchmarkPageLoadingTime(url) { const browser = await puppeteer.launch(); const page = await browser.newPage(); const startTime = new Date().getTime(); await page.goto(url, {waitUntil: 'networkidle2'}); const endTime = new Date().getTime(); console.log(`Page loading time for ${url}: ${endTime - startTime}ms`); await browser.close(); }
  1. Call the benchmarkPageLoadingTime function with the URL of the page you want to benchmark:
benchmarkPageLoadingTime('https://www.example.com');
  1. Run the benchmark.js file using Node.js in your terminal:
node benchmark.js

This will open a new Puppeteer browser, navigate to the specified URL, and measure the page loading time. The loading time will be printed out in the terminal. Repeat this process for any other pages you want to benchmark.