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: "" },
// ]
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: "" },
]);
expect(parseServerTiming("miss, db;dur=, cache;desc=")).toEqual([
{ name: "miss", dur: 0, desc: "" },
{ name: "db", dur: 0, desc: "" },
{ name: "cache", dur: 0, desc: "" },
]);
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" },
]);
expect(parseServerTiming(" cache ; desc = hit ; dur = 23.2 ,db;dur=7 ")).toEqual([
{ name: "cache", dur: 23.2, desc: "hit" },
{ name: "db", dur: 7, desc: "" },
]);
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: "" },
]);
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" },
]);
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]);
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: "" },
]);
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).
I'm here to help with Parse a Server-Timing header. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.