completefrontend

Predict the outputEasy

How long do these connection phases take?

The code takes two navigation entries from the tea shop, as performance.getEntriesByType("navigation")[0] gave them: the tea page on a first visit, and the basket page opened from it a few seconds later. It works out how long the DNS lookup, the TCP handshake, the TLS handshake, and the wait for the first byte took on each page, then logs the page's TTFB beside the sum of those phases. What does it log?

The code
// Two pages of Leaf and Kettle, as their navigation entries reported them:
// ms from the start of each page's navigation.
const teaPage = {
  fetchStart: 2.6,
  domainLookupStart: 3.1,
  domainLookupEnd: 21.7,
  connectStart: 21.7,
  secureConnectionStart: 52.3,
  connectEnd: 85.5,
  requestStart: 85.8,
  responseStart: 241.3,
};
// The basket page, opened from the tea page a few seconds later.
const basket = {
  fetchStart: 1.4,
  domainLookupStart: 1.4,
  domainLookupEnd: 1.4,
  connectStart: 1.4,
  secureConnectionStart: 1.4,
  connectEnd: 1.4,
  requestStart: 1.9,
  responseStart: 96.5,
};
 
const ms = (time) => `${Math.round(time * 10) / 10} ms`;
 
function phases(entry) {
  // Over plain HTTP, secureConnectionStart is 0 and the whole connection is TCP.
  const tlsStart = entry.secureConnectionStart > 0 ? entry.secureConnectionStart : entry.connectEnd;
  return {
    DNS: entry.domainLookupEnd - entry.domainLookupStart,
    TCP: tlsStart - entry.connectStart,
    TLS: entry.connectEnd - tlsStart,
    waiting: entry.responseStart - entry.requestStart,
  };
}
 
for (const [name, entry] of [["Tea page", teaPage], ["Basket", basket]]) {
  const parts = phases(entry);
  console.log(`${name}: ${Object.entries(parts).map(([phase, time]) => `${phase} ${ms(time)}`).join(", ")}`);
  const total = Object.values(parts).reduce((sum, time) => sum + time, 0);
  console.log(`${name}: TTFB ${ms(entry.responseStart)}, of which these phases are ${ms(total)}`);
}

Practice it in the editor, with tests

The answer

Work out every line it logs, in order. Then check.

Checking your sign-in…