Skip to the code

Parse a Server-Timing header

A server can say where its time went with a Server-Timing response header, and the browser hands it to the page as serverTiming on navigation and resource entries. Write parseServerTiming(value), which parses a header's value the way the W3C Server Timing spec does, into an array with one { name, dur, desc } per metric, in order.

Metrics are separated by commas, except commas inside a quoted string. Trim spaces around each, and skip empty ones. A metric is a name followed by parameters, each after a ; (again, a ; inside a quoted string doesn't count). The name is everything before the first ;, trimmed; skip a metric whose name is empty. A parameter is a name, an =, and a value, with spaces allowed around the ; and the =. A value is either plain text up to the next ;, trimmed, or a quoted string: it starts with ", a backslash takes the character after it as it is (so \" is a quote), it ends at the next " that isn't escaped, and anything after it up to the next ; is ignored. A parameter with nothing after its = has the empty string as its value.

Only the first occurrence of a parameter name in a metric counts, even if its value is empty or not a number. dur is the duration in ms: read the value as a number from its start, as parseFloat does (53ms is 53), and use 0 when there's no dur or the result isn't a finite number. desc is the description, or "" when there's none. Ignore every other parameter. Two metrics may have the same name; keep both.

parseServerTiming('cdn;desc=HIT, db;dur=42.5;desc="Tea list", render;dur=18');
// [
//   { name: "cdn", dur: 0, desc: "HIT" },
//   { name: "db", dur: 42.5, desc: "Tea list" },
//   { name: "render", dur: 18, desc: "" },
// ]

What the tests check

  1. parses each metric's name, dur, and desc, in order
    expect(parseServerTiming('cdn;desc=HIT, db;dur=42.5;desc="Tea list", render;dur=18')).toEqual([
      { name: "cdn", dur: 0, desc: "HIT" },
      { name: "db", dur: 42.5, desc: "Tea list" },
      { name: "render", dur: 18, desc: "" },
    ]);
    
  2. a metric without dur or desc gets 0 and an empty string, and so does an empty value
    expect(parseServerTiming("miss, db;dur=, cache;desc=")).toEqual([
      { name: "miss", dur: 0, desc: "" },
      { name: "db", dur: 0, desc: "" },
      { name: "cache", dur: 0, desc: "" },
    ]);
    
  3. a quoted string may hold commas, semicolons, and equals signs, a backslash escapes, and what follows the quote is ignored
    expect(parseServerTiming('menu;desc="Green, mint; oolong=3";dur=12, quote;desc="She said \\"lovely\\" twice", cache;desc="Cache Read"xyz;dur=5')).toEqual([
      { name: "menu", dur: 12, desc: "Green, mint; oolong=3" },
      { name: "quote", dur: 0, desc: 'She said "lovely" twice' },
      { name: "cache", dur: 5, desc: "Cache Read" },
    ]);
    
  4. spaces around the semicolons and equals signs don't matter
    expect(parseServerTiming(" cache ; desc = hit ; dur = 23.2 ,db;dur=7 ")).toEqual([
      { name: "cache", dur: 23.2, desc: "hit" },
      { name: "db", dur: 7, desc: "" },
    ]);
    
  5. only a parameter's first occurrence counts, even when it's empty or not a number
    expect(parseServerTiming("db;dur=53;dur=99, cache;dur=fast;dur=12, edge;desc=;desc=lisbon")).toEqual([
      { name: "db", dur: 53, desc: "" },
      { name: "cache", dur: 0, desc: "" },
      { name: "edge", dur: 0, desc: "" },
    ]);
    
  6. a metric name can repeat, and every one is kept
    expect(parseServerTiming("cache;desc=edge;dur=2, cache;desc=origin;dur=31")).toEqual([
      { name: "cache", dur: 2, desc: "edge" },
      { name: "cache", dur: 31, desc: "origin" },
    ]);
    
  7. dur reads a number from the start of its value, like parseFloat, and anything else is 0
    expect(parseServerTiming("a;dur=12.5ms, b;dur=-3, c;dur=.5, d;dur=1e3, e;dur=Infinity, f;dur=abc").map((metric) => metric.dur)).toEqual([12.5, -3, 0.5, 1000, 0, 0]);
    
  8. skips empty input, empty items, nameless metrics, and parameters it doesn't know
    expect(parseServerTiming("")).toEqual([]);
    expect(parseServerTiming(" , ,")).toEqual([]);
    expect(parseServerTiming("db;dur=5;region=eu, ;dur=9, , app")).toEqual([
      { name: "db", dur: 5, desc: "" },
      { name: "app", dur: 0, desc: "" },
    ]);
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).