Get Chrome DevTools Network Timing Data with Puppeteer
In this article
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
- Set up a new project
Create a folder and initialize it:mkdir puppeteer-timing && cd puppeteer-timing && npm init -y - Install Puppeteer
Runnpm install puppeteer. This downloads a compatible Chrome version. If you already have Chrome installed, you can usepuppeteer-coreand specify an executable path. - Create a script file
Createtiming.jsand open it in your editor. - 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. - Enable the Network domain
Puppeteer's CDP session allows you to enable the Network domain, which is required to capture timing events. Useconst client = await page.target().createCDPSession();and thenawait client.send('Network.enable'); - Listen for the
Network.responseReceivedevent
This event fires for every response and contains aresponse.timingobject. Add an event listener:client.on('Network.responseReceived', (event) => { ... }); - Extract timing data
Inside the listener, accessevent.response.timingand log it. The timing object has properties likerequestTime,dnsStart,dnsEnd,connectStart,connectEnd,sslStart,sslEnd,sendStart,sendEnd,receiveHeadersEnd. Also grab the URL fromevent.response.url. - 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). - Wait for all responses
Since network events fire asynchronously, useawait page.goto(url, { waitUntil: 'networkidle0' });to ensure all requests are completed before processing the collected data. - Store and output the data
Collect timing entries in an array, then after the page loads, print them or save to a JSON file. - Close the browser
Always callawait browser.close();to free resources.
Common Mistakes
- Forgetting to enable the Network domain – Without
Network.enable, theresponseReceivedevent never fires. - Assuming timing values are all in milliseconds –
requestTimeis in seconds (monotonic time), while all other timings are in milliseconds relative to that. Always convertrequestTimeto 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/sslEndfor 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
XHRorFetchrequests to avoid noise. - Use
Promise.allwith 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.enablebeforepage.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. Usenetworkidle2or a manual timeout. - Puppeteer fails to launch – On Linux, you may need to install additional dependencies. Run
npx puppeteer browsers install chromeor 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
timingbeing 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)).