completefrontend

Write the codeMedium

Write a function that turns a navigation entry into a waterfall of phases.

Leaf and Kettle's RUM dashboard draws each slow visit as a waterfall. Write navigationWaterfall(entry). entry is the page's navigation entry, performance.getEntriesByType("navigation")[0], with its times in ms from the start of the navigation. Return its phases as { name, start, duration } objects, in the order Navigation Timing 2 draws them:

name             from                     to
redirect         redirectStart            redirectEnd
service worker   workerStart              fetchStart
cache            fetchStart               domainLookupStart
DNS              domainLookupStart        domainLookupEnd
TCP              connectStart             secureConnectionStart
TLS              secureConnectionStart    connectEnd
request          requestStart             responseStart
response         responseStart            responseEnd
processing       responseEnd              domComplete
load event       loadEventStart           loadEventEnd

A field the browser has nothing for is 0. The service worker phase only exists when workerStart is above 0. Without TLS, secureConnectionStart is 0, so TCP runs from connectStart to connectEnd and there's no TLS phase. Round start and duration (the end minus the start) to the nearest 0.1 ms with Math.round(ms * 10) / 10, and leave out every phase whose rounded duration isn't above 0: one that didn't happen, one that took no time, like DNS on a reused connection, and one that hadn't finished when the entry was read (domComplete and the load event's times are 0 until then).

navigationWaterfall(performance.getEntriesByType("navigation")[0]);
// A visit over a reused connection: fetchStart through connectEnd all 0.4, requestStart 0.9,
// responseStart 61.2, responseEnd 75, domComplete 190.4, loadEventStart 190.5, loadEventEnd 190.9
// [
//   { name: "request", start: 0.9, duration: 60.3 },
//   { name: "response", start: 61.2, duration: 13.8 },
//   { name: "processing", start: 75, duration: 115.4 },
//   { name: "load event", start: 190.5, duration: 0.4 },
// ]
Write this: solution.js
function navigationWaterfall(entry) {
  // Today every phase is drawn, even the ones that didn't happen, TLS is hidden inside the connection, and nothing is rounded.
  return [
    { name: "redirect", start: entry.redirectStart, duration: entry.redirectEnd - entry.redirectStart },
    { name: "DNS", start: entry.domainLookupStart, duration: entry.domainLookupEnd - entry.domainLookupStart },
    { name: "connection", start: entry.connectStart, duration: entry.connectEnd - entry.connectStart },
    { name: "request", start: entry.requestStart, duration: entry.responseStart - entry.requestStart },
    { name: "response", start: entry.responseStart, duration: entry.responseEnd - entry.responseStart },
  ];
}
The tests your code must pass
  • a first visit over a new HTTPS connection, phase by phase
  • a reused connection has no DNS, TCP, or TLS phases
  • over plain HTTP, TCP runs to connectEnd and there's no TLS phase
  • a same-origin redirect comes first, and a hidden one leaves no phase
  • a service worker's start-up is a phase, and only when there is one
  • an entry read before the page finished loading has no processing or load event phase
  • rounds to a tenth of a millisecond and leaves out a phase that rounds to 0

Try it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…