A HAR (HTTP Archive) file is the JSON a browser's Network panel exports: every request of a page load, with its sizes and timings. Write harSummary(har). har.log.entries are the requests, each with request.url and a response that has headersSize, bodySize, and content.mimeType, and in Chrome's HAR files _transferSize too. Return { requests, bytes, byType, largest }.
A request's bytes are what came over the network. The HAR 1.2 spec gives response.headersSize (the headers as received) and response.bodySize (the body as received, 0 for a response that came from the cache, such as a 304), each -1 when it isn't known, and the response's total is their sum: count a part that's -1 as 0. Chrome also writes _transferSize (HAR's custom fields start with an underscore), the whole response as it crossed the network. When _transferSize is a number, use it instead.
Group requests by response.content.mimeType, without its parameters and in lowercase (text/html; charset=utf-8 is text/html): html for text/html; css for text/css; js for text/javascript, application/javascript, or application/x-javascript; json for application/json or any type that ends in +json; image for image/...; font for font/...; media for video/... or audio/...; and other for anything else, a missing type included. byType has { requests, bytes } for each group that has at least one request.
requests is the number of entries and bytes their total. largest is the three entries with the most bytes, as { url, type, bytes } where type is the group, largest first, and the earlier entry first when two tie. It has fewer when there are fewer entries.
harSummary({ log: { entries: [
{ request: { url: "https://leafandkettle.com/" }, response: { headersSize: 400, bodySize: 14600, _transferSize: 15000, content: { mimeType: "text/html; charset=utf-8" } } },
{ request: { url: "https://leafandkettle.com/js/app.js" }, response: { headersSize: 300, bodySize: 88000, content: { mimeType: "text/javascript" } } },
{ request: { url: "https://leafandkettle.com/img/oolong.avif" }, response: { headersSize: -1, bodySize: 42000, content: { mimeType: "image/avif" } } },
{ request: { url: "https://leafandkettle.com/api/cart" }, response: { headersSize: -1, bodySize: -1, _transferSize: 250, content: { mimeType: "application/json" } } },
] } });
// {
// requests: 4,
// bytes: 145550,
// byType: { html: { requests: 1, bytes: 15000 }, js: { requests: 1, bytes: 88300 }, image: { requests: 1, bytes: 42000 }, json: { requests: 1, bytes: 250 } },
// largest: [
// { url: "https://leafandkettle.com/js/app.js", type: "js", bytes: 88300 },
// { url: "https://leafandkettle.com/img/oolong.avif", type: "image", bytes: 42000 },
// { url: "https://leafandkettle.com/", type: "html", bytes: 15000 },
// ],
// }
const entry = (url, mimeType, sizes) => ({ request: { method: "GET", url }, response: { status: 200, content: { mimeType }, ...sizes } });
const summary = harSummary({ log: { entries: [
entry("https://leafandkettle.com/", "text/html", { headersSize: 400, bodySize: 14600 }),
entry("https://leafandkettle.com/js/app.js", "text/javascript", { headersSize: 300, bodySize: 88000 }),
entry("https://leafandkettle.com/css/site.css", "text/css", { headersSize: 280, bodySize: 12000 }),
] } });
expect(summary.requests).toBe(3);
expect(summary.bytes).toBe(115580);
expect(summary.byType).toEqual({ html: { requests: 1, bytes: 15000 }, js: { requests: 1, bytes: 88300 }, css: { requests: 1, bytes: 12280 } });
const entry = (url, mimeType, sizes) => ({ request: { method: "GET", url }, response: { status: 200, content: { mimeType }, ...sizes } });
const summary = harSummary({ log: { entries: [
entry("https://leafandkettle.com/js/app.js", "text/javascript", { headersSize: -1, bodySize: -1, _transferSize: 25000 }),
entry("https://leafandkettle.com/js/menu.js", "text/javascript", { headersSize: 320, bodySize: 0, _transferSize: 0 }),
entry("https://leafandkettle.com/css/site.css", "text/css", { headersSize: 300, bodySize: 12000 }),
] } });
expect(summary.requests).toBe(3);
expect(summary.bytes).toBe(37300);
expect(summary.byType).toEqual({ js: { requests: 2, bytes: 25000 }, css: { requests: 1, bytes: 12300 } });
const entry = (url, mimeType, sizes) => ({ request: { method: "GET", url }, response: { status: 200, content: { mimeType }, ...sizes } });
const summary = harSummary({ log: { entries: [
entry("https://leafandkettle.com/img/oolong.avif", "image/avif", { headersSize: -1, bodySize: 42000 }),
entry("https://leafandkettle.com/img/mint.avif", "image/avif", { headersSize: 250, bodySize: -1 }),
entry("https://leafandkettle.com/img/green.avif", "image/avif", { headersSize: -1, bodySize: -1 }),
entry("https://leafandkettle.com/css/site.css", "text/css", { status: 304, headersSize: 180, bodySize: 0 }),
] } });
expect(summary.bytes).toBe(42430);
expect(summary.byType).toEqual({ image: { requests: 3, bytes: 42250 }, css: { requests: 1, bytes: 180 } });
const entry = (url, mimeType, bodySize) => ({ request: { method: "GET", url }, response: { status: 200, headersSize: 0, bodySize, content: mimeType === undefined ? {} : { mimeType } } });
const types = [
["text/html; charset=utf-8", 1000], ["Text/CSS", 2000], ["application/javascript; charset=utf-8", 3000], ["application/x-javascript", 500],
["application/json", 400], ["application/manifest+json", 600], ["image/svg+xml", 700], ["image/avif", 800], ["font/woff2", 900],
["video/mp4", 5000], ["audio/mpeg", 100], ["x-unknown", 50], ["text/plain", 25], [undefined, 10], ["application/wasm", 1200],
];
const summary = harSummary({ log: { entries: types.map(([type, size], index) => entry("https://leafandkettle.com/file-" + index, type, size)) } });
expect(summary.byType).toEqual({
html: { requests: 1, bytes: 1000 },
css: { requests: 1, bytes: 2000 },
js: { requests: 2, bytes: 3500 },
json: { requests: 2, bytes: 1000 },
image: { requests: 2, bytes: 1500 },
font: { requests: 1, bytes: 900 },
media: { requests: 2, bytes: 5100 },
other: { requests: 4, bytes: 1285 },
});
const entry = (url, mimeType, bodySize) => ({ request: { method: "GET", url }, response: { status: 200, headersSize: 0, bodySize, content: { mimeType } } });
const summary = harSummary({ log: { entries: [
entry("https://leafandkettle.com/img/green.avif", "image/avif", 5000),
entry("https://leafandkettle.com/js/app.js", "text/javascript", 9000),
entry("https://leafandkettle.com/img/mint.avif", "image/avif", 5000),
entry("https://leafandkettle.com/css/site.css", "text/css", 2000),
entry("https://leafandkettle.com/js/vendor.js", "text/javascript", 9000),
] } });
expect(summary.largest).toEqual([
{ url: "https://leafandkettle.com/js/app.js", type: "js", bytes: 9000 },
{ url: "https://leafandkettle.com/js/vendor.js", type: "js", bytes: 9000 },
{ url: "https://leafandkettle.com/img/green.avif", type: "image", bytes: 5000 },
]);
const one = { request: { method: "GET", url: "https://leafandkettle.com/" }, response: { status: 200, headersSize: 400, bodySize: 14600, content: { mimeType: "text/html" } } };
expect(harSummary({ log: { entries: [one] } }).largest).toEqual([{ url: "https://leafandkettle.com/", type: "html", bytes: 15000 }]);
expect(harSummary({ log: { entries: [] } })).toEqual({ requests: 0, bytes: 0, byType: {}, largest: [] });
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 a HAR file. 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.