User Agents

Web browsers and everyday web tech, explained

Get Chrome DevTools Network Timing Data with Puppeteer

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to capture Chrome DevTools network timing data (like DNS lookup, TCP connection, SSL negotiation, time to first byte, and content download) using Puppeteer. This is useful for performance auditing, measuring API latency, or debugging slow-loading resources. You'll get the exact same timing breakdown you see in the DevTools Network tab, but programmatically.

What You'll Need

  • Node.js (version 14 or later recommended)
  • npm or yarn
  • A basic understanding of JavaScript and async/await

Step-by-Step

  1. Set up a new project
    Create a folder and initialize it: mkdir puppeteer-timing && cd puppeteer-timing && npm init -y
  2. Install Puppeteer
    Run npm install puppeteer. This downloads a compatible Chrome version. If you already have Chrome installed, you can use puppeteer-core and specify an executable path.
  3. Create a script file
    Create timing.js and open it in your editor.
  4. Launch a browser and open a page
    Write the basic boilerplate: const puppeteer = require('puppeteer'); then inside an async function, launch the browser, open a new page, and navigate to a URL.
  5. Enable the Network domain
    Puppeteer's CDP session allows you to enable the Network domain, which is required to capture timing events. Use const client = await page.target().createCDPSession(); and then await client.send('Network.enable');
  6. Listen for the Network.responseReceived event
    This event fires for every response and contains a response.timing object. Add an event listener: client.on('Network.responseReceived', (event) => { ... });
  7. Extract timing data
    Inside the listener, access event.response.timing and log it. The timing object has properties like requestTime, dnsStart, dnsEnd, connectStart, connectEnd, sslStart, sslEnd, sendStart, sendEnd, receiveHeadersEnd. Also grab the URL from event.response.url.
  8. Calculate durations
    Compute meaningful metrics: DNS duration = dnsEnd - dnsStart, TCP connection = connectEnd - connectStart, SSL = sslEnd - sslStart, time to first byte (TTFB) = receiveHeadersEnd - requestTime (note: requestTime is in seconds, others are in milliseconds, so convert).
  9. Wait for all responses
    Since network events fire asynchronously, use await page.goto(url, { waitUntil: 'networkidle0' }); to ensure all requests are completed before processing the collected data.
  10. Store and output the data
    Collect timing entries in an array, then after the page loads, print them or save to a JSON file.
  11. Close the browser
    Always call await browser.close(); to free resources.

Common Mistakes

  • Forgetting to enable the Network domain – Without Network.enable, the responseReceived event never fires.
  • Assuming timing values are all in milliseconds – requestTime is in seconds (monotonic time), while all other timings are in milliseconds relative to that. Always convert requestTime to ms by multiplying by 1000 before subtracting.
  • Not waiting for network idle – If you close the browser too early, you may miss late responses. Use waitUntil: 'networkidle0' or a similar condition.
  • Misinterpreting sslStart/sslEnd for non-HTTPS – For HTTP URLs, these values are 0. Check the protocol before assuming SSL time.
  • Using page.on('response') instead of CDP – The Puppeteer response object does not expose timing; you must use CDP.

Tips

  • Filter by resource type – Only log timing for XHR or Fetch requests to avoid noise.
  • Use Promise.all with a timeout – If you have many requests, consider collecting them in a promise that resolves after a fixed delay.
  • Compare multiple runs – Run the script multiple times and average the timings for more reliable performance metrics.
  • Handle redirects – Each redirect will produce its own response with its own timing. Decide whether you want all or just the final one.
  • Use page.setCacheEnabled(false) – To measure uncached load times, disable the cache before navigation.

Troubleshooting

  • No timing data appears – Ensure you called Network.enable before page.goto. Also check that your URL is reachable.
  • Timing values are all zero – This can happen if the response is served from the browser cache. Disable cache or add a cache-busting query parameter.
  • Script hangs – If you use networkidle0, some pages with continuous requests may never reach idle. Use networkidle2 or a manual timeout.
  • Puppeteer fails to launch – On Linux, you may need to install additional dependencies. Run npx puppeteer browsers install chrome or check the official troubleshooting docs.
  • Events fire but data is incomplete – Some responses (like service workers) may not have full timing. Filter out entries with timing being null.

Frequently Asked Questions

Can I get the same timing data as the DevTools Network tab?

Yes, the CDP timing object is exactly what DevTools displays. You get the same breakdown.

Do I need to use puppeteer-core instead of puppeteer?

No, the standard puppeteer package works fine. Use puppeteer-core only if you want to use an existing Chrome installation.

How do I get timing for a specific request?

In the Network.responseReceived listener, filter by event.response.url or event.requestId.

Can I measure WebSocket timing?

No, the Network domain does not provide timing for WebSocket connections. You would need to instrument the client-side code.

Is there a way to get resource timing without CDP?

Yes, you can use page.evaluate(() => performance.getEntriesByType('resource')) to get similar data, but it lacks some low-level details like DNS or SSL breakdown.

How do I save the data to a file?

Collect the timing objects in an array, then use fs.writeFileSync('timings.json', JSON.stringify(data, null, 2)).