The tea shop times its slow spots with performance.measure(): menu:render each time the menu draws, search each time someone searches. Write summarizeMeasures(), which reads every measure entry from performance.getEntriesByType("measure") and returns one row per measure name, { name, count, min, max, mean, p75 }, from the entries' durations in ms.
count is how many measures have that name, min and max the shortest and the longest duration, mean their average, and p75 the 75th percentile by the nearest-rank rule of the p75-of-samples problem: sort the durations from shortest to longest and take the one at position Math.ceil(0.75 * count), counting from 1.
Put the rows slowest first, by p75, and rows with the same p75 in alphabetical order of their names (compared with <). Return an empty array when there are no measures. Leave the timeline as it was: other code may read the same entries later.
// The menu drew four times, in 40, 10, 30, and 20 ms. One search took 120 ms.
summarizeMeasures();
// [
// { name: "search", count: 1, min: 120, max: 120, mean: 120, p75: 120 },
// { name: "menu:render", count: 4, min: 10, max: 40, mean: 25, p75: 30 },
// ]
usePerformance();
[40, 10, 30, 20].forEach((duration, index) => performance.measure("menu:render", { start: 1000 * (index + 1), duration }));
expect(summarizeMeasures()).toEqual([{ name: "menu:render", count: 4, min: 10, max: 40, mean: 25, p75: 30 }]);
usePerformance();
const time = (name, durations) => durations.forEach((duration, index) => performance.measure(name, { start: 100 * (index + 1), duration }));
time("menu:render", [40, 10, 30, 20]);
time("basket:open", [8]);
time("search", [120]);
time("cart:total", [30, 20]);
expect(summarizeMeasures()).toEqual([
{ name: "search", count: 1, min: 120, max: 120, mean: 120, p75: 120 },
{ name: "cart:total", count: 2, min: 20, max: 30, mean: 25, p75: 30 },
{ name: "menu:render", count: 4, min: 10, max: 40, mean: 25, p75: 30 },
{ name: "basket:open", count: 1, min: 8, max: 8, mean: 8, p75: 8 },
]);
usePerformance();
const time = (name, durations) => durations.forEach((duration, index) => performance.measure(name, { start: 100 * (index + 1), duration }));
time("filters:apply", [7]);
time("photos:decode", [5, 1, 4, 2, 3]);
time("reviews:render", [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]);
expect(summarizeMeasures().map((row) => [row.name, row.p75])).toEqual([
["reviews:render", 80],
["filters:apply", 7],
["photos:decode", 4],
]);
const perf = usePerformance();
perf.navigation({ responseStart: 120, loadEventEnd: 900 });
perf.buffer("resource", [{ name: "https://example.test/app.js", initiatorType: "script", startTime: 200, duration: 80 }]);
performance.mark("app:ready", { startTime: 950 });
performance.measure("menu:render", { start: 1000, duration: 24 });
expect(summarizeMeasures()).toEqual([{ name: "menu:render", count: 1, min: 24, max: 24, mean: 24, p75: 24 }]);
expect(performance.getEntries().map((entry) => entry.entryType)).toEqual(["navigation", "resource", "mark", "measure"]);
usePerformance();
performance.mark("app:ready", { startTime: 950 });
expect(summarizeMeasures()).toEqual([]);
usePerformance();
[2, 0, 1.5, 0.5].forEach((duration, index) => performance.measure("price:format", { start: 10 * (index + 1), duration }));
expect(summarizeMeasures()).toEqual([{ name: "price:format", count: 4, min: 0, max: 2, mean: 1, p75: 1.5 }]);
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 Summarize User Timing measures. 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.