Write resourceSummary(pageOrigin), which sums up what the page fetched from its Resource Timing entries, performance.getEntriesByType("resource"). pageOrigin is the page's origin, like "https://leafandkettle.example"; on a real page you'd pass location.origin.
Each entry has a name, the file's URL; an initiatorType, what asked for it ("script", "img", "css", "link", "fetch", and so on); timestamps such as requestStart and responseStart; and three sizes in bytes: transferSize, what crossed the network (the Resource Timing spec counts 300 bytes for the headers), encodedBodySize, the body as sent, and decodedBodySize, the body after decompression.
Return { requests, transferSize, byType, cached, opaque }. requests is the number of entries, and transferSize the sum of theirs. byType has a key for each initiatorType seen, holding { count, transferSize } for its entries.
opaque lists the URLs of entries from another origin whose responseStart is 0. Another origin means new URL(name).origin isn't pageOrigin, so a subdomain, another port, or http instead of https counts. Those files' servers sent no Timing-Allow-Origin header naming the page (or *), so the spec hides their detailed timings, and Chrome reports their sizes as 0 too. cached lists the URLs of the other entries that came from the browser's HTTP cache: a transferSize of 0 with a decodedBodySize above 0, since nothing crossed the network but there is a body. A revalidated file, which cost a round trip for a 304, has a transferSize of 300, so it isn't a cache hit. Both lists keep the timeline's order.
resourceSummary("https://leafandkettle.example");
// {
// requests: 4,
// transferSize: 48900,
// byType: {
// script: { count: 1, transferSize: 48600 }, // app.js, over the network
// link: { count: 1, transferSize: 0 }, // menu.css, from the cache
// img: { count: 1, transferSize: 0 }, // a CDN's photo, without Timing-Allow-Origin
// fetch: { count: 1, transferSize: 300 }, // menu.json, revalidated
// },
// cached: ["https://leafandkettle.example/css/menu.css"],
// opaque: ["https://cdn.example.net/teas/oolong.avif"],
// }
const perf = usePerformance();
const resource = (name, initiatorType, startTime, [transferSize, encodedBodySize, decodedBodySize], responseStart = startTime + 30) => ({
name, initiatorType, startTime, duration: 60, transferSize, encodedBodySize, decodedBodySize,
requestStart: responseStart && startTime + 10, responseStart, responseEnd: startTime + 60,
});
const shop = "https://leafandkettle.example";
perf.buffer("resource", [
resource(shop + "/js/app.js", "script", 100, [48600, 48300, 160000]),
resource(shop + "/css/menu.css", "link", 110, [9300, 9000, 41000]),
resource(shop + "/js/vendor.js", "script", 120, [30300, 30000, 98000]),
resource(shop + "/teas/oolong.avif", "img", 400, [64300, 64000, 64000]),
resource(shop + "/api/menu.json", "fetch", 800, [2300, 2000, 7400]),
]);
expect(resourceSummary(shop)).toEqual({
requests: 5,
transferSize: 154800,
byType: {
script: { count: 2, transferSize: 78900 },
link: { count: 1, transferSize: 9300 },
img: { count: 1, transferSize: 64300 },
fetch: { count: 1, transferSize: 2300 },
},
cached: [],
opaque: [],
});
const perf = usePerformance();
const resource = (name, initiatorType, startTime, [transferSize, encodedBodySize, decodedBodySize], responseStart = startTime + 30) => ({
name, initiatorType, startTime, duration: 60, transferSize, encodedBodySize, decodedBodySize,
requestStart: responseStart && startTime + 10, responseStart, responseEnd: startTime + 60,
});
const shop = "https://leafandkettle.example";
perf.buffer("resource", [
resource(shop + "/css/menu.css", "link", 110, [0, 9000, 41000]),
resource(shop + "/api/menu.json", "fetch", 800, [300, 2000, 7400]),
resource(shop + "/js/app.js", "script", 100, [0, 48300, 160000]),
]);
const summary = resourceSummary(shop);
expect(summary.cached).toEqual([shop + "/js/app.js", shop + "/css/menu.css"]);
expect(summary.transferSize).toBe(300);
expect(summary.opaque).toEqual([]);
const perf = usePerformance();
const resource = (name, initiatorType, startTime, [transferSize, encodedBodySize, decodedBodySize], responseStart = startTime + 30) => ({
name, initiatorType, startTime, duration: 60, transferSize, encodedBodySize, decodedBodySize,
requestStart: responseStart && startTime + 10, responseStart, responseEnd: startTime + 60,
});
const shop = "https://leafandkettle.example";
perf.buffer("resource", [
resource("https://cdn.example.net/teas/oolong.avif", "img", 400, [0, 0, 0], 0),
resource("https://fonts.example.net/lora.woff2", "css", 250, [0, 18000, 18000]), // allowed, and cached
]);
const summary = resourceSummary(shop);
expect(summary.opaque).toEqual(["https://cdn.example.net/teas/oolong.avif"]);
expect(summary.cached).toEqual(["https://fonts.example.net/lora.woff2"]);
expect(summary.byType).toEqual({ css: { count: 1, transferSize: 0 }, img: { count: 1, transferSize: 0 } });
const perf = usePerformance();
const resource = (name, initiatorType, startTime, [transferSize, encodedBodySize, decodedBodySize], responseStart = startTime + 30) => ({
name, initiatorType, startTime, duration: 60, transferSize, encodedBodySize, decodedBodySize,
requestStart: responseStart && startTime + 10, responseStart, responseEnd: startTime + 60,
});
const shop = "https://leafandkettle.example";
perf.buffer("resource", [
resource("https://img.leafandkettle.example/teas/mint.avif", "img", 100, [0, 0, 0], 0),
resource("https://leafandkettle.example:8443/js/chat.js", "script", 200, [0, 0, 0], 0),
resource("http://leafandkettle.example/css/print.css", "link", 300, [0, 0, 0], 0),
resource(shop + "/api/stock.json", "fetch", 400, [0, 0, 0], 0), // a request that failed
]);
expect(resourceSummary(shop).opaque).toEqual([
"https://img.leafandkettle.example/teas/mint.avif",
"https://leafandkettle.example:8443/js/chat.js",
"http://leafandkettle.example/css/print.css",
]);
expect(resourceSummary(shop).cached).toEqual([]);
const perf = usePerformance();
perf.navigation({ responseStart: 120, transferSize: 18300, encodedBodySize: 18000, decodedBodySize: 64000, loadEventEnd: 900 });
performance.mark("app:ready", { startTime: 950 });
perf.buffer("resource", [{ name: "https://leafandkettle.example/js/app.js", initiatorType: "script", startTime: 100, duration: 60, transferSize: 48600, encodedBodySize: 48300, decodedBodySize: 160000, requestStart: 110, responseStart: 130, responseEnd: 160 }]);
expect(resourceSummary("https://leafandkettle.example")).toEqual({
requests: 1,
transferSize: 48600,
byType: { script: { count: 1, transferSize: 48600 } },
cached: [],
opaque: [],
});
usePerformance();
expect(resourceSummary("https://leafandkettle.example")).toEqual({ requests: 0, transferSize: 0, byType: {}, cached: [], opaque: [] });
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 Resource Timing. 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.