Navigation Timing API
What you'll learn: How to use performance.timing to measure specific phases of page load like DNS lookup, TCP connection, and time to first byte.
What Is the Navigation Timing API?
The Navigation Timing API gives you programmatic access to precise timing data about how your page loaded. Think of it like a detailed receipt from a restaurant that breaks down every charge—instead of just knowing your page took 3 seconds to load, you can see exactly how much time was spent on DNS lookup, establishing the TCP connection, waiting for the server's response, and more.
The performance.timing Object
The performance.timing object contains timestamps (in milliseconds since the Unix epoch) for key navigation events:
// Access timing data
const timing = performance.timing;
// Calculate DNS lookup time
const dnsTime = timing.domainLookupEnd - timing.domainLookupStart;
// Calculate TCP connection time
const tcpTime = timing.connectEnd - timing.connectStart;
// Calculate Time to First Byte (TTFB)
const ttfb = timing.responseStart - timing.navigationStart;
// Calculate page load time
const pageLoadTime = timing.loadEventEnd - timing.navigationStart;
console.log(`DNS: ${dnsTime}ms`);
console.log(`TCP: ${tcpTime}ms`);
console.log(`TTFB: ${ttfb}ms`);
console.log(`Total: ${pageLoadTime}ms`);
Key Timing Phases
By subtracting timestamp pairs, you isolate specific phases:
- DNS:
domainLookupEnd - domainLookupStart - TCP:
connectEnd - connectStart - Request/Response:
responseEnd - requestStart - DOM Processing:
domInteractive - domLoading
This granular data helps you identify bottlenecks—maybe your TTFB is slow (server issue) or DNS lookups are taking too long (DNS provider issue).
Key Takeaway: The Navigation Timing API's performance.timing object provides timestamp data that lets you measure and analyze specific page load phases, helping you pinpoint exactly where performance problems occur.