-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathJS-Execution-Time-Breakdown.js
More file actions
336 lines (294 loc) · 13.8 KB
/
JS-Execution-Time-Breakdown.js
File metadata and controls
336 lines (294 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// JavaScript Execution Time Breakdown
// https://webperf-snippets.nucliweb.net
(() => {
const formatMs = (ms) => (ms > 0 ? ms.toFixed(0) + "ms" : "-");
const formatBytes = (bytes) => {
if (!bytes || bytes === 0) return "-";
const k = 1024;
const sizes = ["B", "KB", "MB"];
const i = Math.floor(Math.log(Math.max(bytes, 1)) / Math.log(k));
return (bytes / Math.pow(k, i)).toFixed(1) + " " + sizes[i];
};
const formatBar = (value, max, width = 16) => {
if (value <= 0 || max <= 0) return "░".repeat(width);
const filled = Math.min(Math.round((value / max) * width), width);
return "█".repeat(filled) + "░".repeat(width - filled);
};
// 1. Page load timeline
const nav = performance.getEntriesByType("navigation")[0];
const domInteractive = nav?.domInteractive || 0;
const domContentLoaded = nav?.domContentLoadedEventEnd || 0;
const loadEvent = nav?.loadEventEnd || 0;
// 2. Script resource timing (download phase)
const scriptResources = performance
.getEntriesByType("resource")
.filter((r) => r.initiatorType === "script")
.map((r) => {
let shortName;
try {
const url = new URL(r.name);
shortName = url.pathname.split("/").pop() || url.hostname;
} catch {
shortName = r.name.split("/").pop() || r.name;
}
const transferSize = r.transferSize || 0;
const decodedSize = r.decodedBodySize || 0;
const downloadDuration = Math.round(r.responseEnd - r.startTime);
// CORS-restricted: third-party scripts without Timing-Allow-Origin return 0 for sizes
const corsRestricted = transferSize === 0 && decodedSize === 0 && downloadDuration > 0;
// Parse time estimates based on V8 data (v8.dev/blog/cost-of-javascript-2019)
// Mobile (~1ms/KB): Moto G4 class device — worst-case baseline
// Desktop (~0.3ms/KB): ~3x faster than mobile baseline
const estimatedParseMobile = Math.round(decodedSize / 1024);
const estimatedParseDesktop = Math.round(decodedSize / 1024 / 3);
return {
name: r.name,
shortName: shortName.split("?")[0] || shortName,
startTime: r.startTime,
responseEnd: r.responseEnd,
downloadDuration,
transferSize,
decodedSize,
corsRestricted,
estimatedParseMobile,
estimatedParseDesktop,
totalCostMs: downloadDuration + estimatedParseMobile,
};
})
.sort((a, b) => a.startTime - b.startTime);
// 3. Detect render-blocking scripts (in <head>, no async/defer/module)
const blockingSet = new Set();
Array.from(document.querySelectorAll("script[src]")).forEach((el) => {
const isModule = el.type === "module";
if (!el.async && !el.defer && !isModule && el.closest("head")) {
blockingSet.add(el.src);
}
});
const scripts = scriptResources.map((s) => ({
...s,
isBlocking: blockingSet.has(s.name),
}));
// 4. Summary calculations
const blocking = scripts.filter((s) => s.isBlocking);
const nonBlocking = scripts.filter((s) => !s.isBlocking);
const totalDownload = scripts.reduce((sum, s) => sum + s.downloadDuration, 0);
const totalEstParse = scripts.reduce((sum, s) => sum + s.estimatedParseMobile, 0);
const totalSize = scripts.reduce((sum, s) => sum + s.transferSize, 0);
const totalDecodedSize = scripts.reduce((sum, s) => sum + s.decodedSize, 0);
// Estimate how much blocking scripts delay domInteractive
// The critical path ends when the last blocking script finishes download + parse
const blockingCriticalPathEnd = blocking.reduce((max, s) => {
const estimatedDone = s.responseEnd + s.estimatedParseMobile;
return Math.max(max, estimatedDone);
}, 0);
const jsBlockingDelay =
blocking.length > 0 && domInteractive > 0
? Math.min(blockingCriticalPathEnd, domInteractive)
: 0;
// Large script threshold (50KB compressed = good candidate for splitting)
const SPLIT_THRESHOLD = 50 * 1024;
const splitCandidates = scripts.filter((s) => s.decodedSize > SPLIT_THRESHOLD);
// 5. Output
console.group(
"%c⚡ JavaScript Execution Time Breakdown",
"font-weight: bold; font-size: 14px;"
);
// Page load timeline
console.log("");
console.log("%c📅 Page Load Timeline:", "font-weight: bold;");
if (domInteractive > 0) {
const timelineMax = loadEvent || domContentLoaded || domInteractive;
const rows = [
{ label: "Navigation start", time: 0 },
{ label: "DOM Interactive", time: domInteractive },
{ label: "DOM Content Loaded", time: domContentLoaded },
{ label: "Load event", time: loadEvent },
].filter((r) => r.time > 0);
rows.forEach(({ label, time }) => {
const bar = formatBar(time, timelineMax, 20);
console.log(` ${label.padEnd(22)} ${formatMs(time).padStart(8)} ${bar}`);
});
if (domInteractive > 0) {
const jsPct =
domInteractive > 0 ? ((jsBlockingDelay / domInteractive) * 100).toFixed(0) : 0;
console.log("");
console.log(
` JS blocking scripts contribute ~${jsPct}% of domInteractive (${formatMs(jsBlockingDelay)} est.)`
);
}
} else {
console.log(" %c⚠️ Navigation Timing unavailable", "color: #f59e0b;");
}
// Script overview
console.log("");
console.log("%c📊 Script Overview:", "font-weight: bold;");
console.log(` Total scripts: ${scripts.length}`);
console.log(` Render-blocking: ${blocking.length}`);
console.log(` Non-blocking: ${nonBlocking.length}`);
console.log(` Total transfer size: ${formatBytes(totalSize)}`);
console.log(` Total decoded size: ${formatBytes(totalDecodedSize)}`);
console.log(` Compression ratio: ${totalDecodedSize > 0 && totalSize > 0 ? (totalDecodedSize / totalSize).toFixed(1) + "x" : "-"}`);
console.log("");
console.log("%c Cost Estimates (download + parse):", "color: #6b7280;");
console.log(` Total download time: ${formatMs(totalDownload)}`);
console.log(` Est. total parse time: ${formatMs(totalEstParse)} (~1ms/KB decoded)`);
console.log(` Est. total JS cost: ${formatMs(totalDownload + totalEstParse)}`);
// Per-script breakdown table
if (scripts.length > 0) {
console.log("");
console.group("%c📋 Per-Script Breakdown", "color: #3b82f6; font-weight: bold;");
console.log("%c📱 Parse estimates: mobile ~1ms/KB · desktop ~0.3ms/KB (source: v8.dev/blog/cost-of-javascript-2019)", "color: #6b7280;");
console.log("");
const corsCount = scripts.filter((s) => s.corsRestricted).length;
if (corsCount > 0) {
console.log(
`%c⚠️ ${corsCount} script(s) show "CORS" — third-party size data blocked by missing Timing-Allow-Origin header`,
"color: #f59e0b;"
);
console.log(" Parse columns unavailable for these scripts.");
console.log("");
}
const maxCost = Math.max(...scripts.map((s) => s.totalCostMs));
const tableData = scripts
.sort((a, b) => b.totalCostMs - a.totalCostMs)
.slice(0, 20)
.map((s) => {
const name = s.shortName.length > 35 ? s.shortName.slice(0, 32) + "..." : s.shortName;
return {
"": s.isBlocking ? "🔴" : "✅",
Script: name,
Download: formatMs(s.downloadDuration),
"Parse(mob)": s.corsRestricted ? "CORS" : formatMs(s.estimatedParseMobile),
"Parse(dsk)": s.corsRestricted ? "CORS" : formatMs(s.estimatedParseDesktop),
"Transfer": s.corsRestricted ? "CORS" : formatBytes(s.transferSize),
"Decoded": s.corsRestricted ? "CORS" : formatBytes(s.decodedSize),
"": formatBar(s.totalCostMs, maxCost),
};
});
console.table(tableData);
if (scripts.length > 20) {
console.log(` ... and ${scripts.length - 20} more scripts`);
}
// Critical bundle warning (> 1 MB decoded)
const criticalBundles = scripts.filter((s) => !s.corsRestricted && s.decodedSize > 1024 * 1024);
if (criticalBundles.length > 0) {
console.log("");
console.log("%c🚨 Critically large bundles (> 1 MB decoded):", "color: #ef4444; font-weight: bold;");
criticalBundles
.sort((a, b) => b.decodedSize - a.decodedSize)
.forEach((s) => {
console.log(` ${s.shortName}`);
console.log(` Decoded: ${formatBytes(s.decodedSize)} | Mobile parse: ~${formatMs(s.estimatedParseMobile)} | Desktop parse: ~${formatMs(s.estimatedParseDesktop)}`);
console.log(" → Split with dynamic import() — only load what the current route needs");
});
}
console.groupEnd();
}
// Blocking scripts TTI impact
if (blocking.length > 0) {
console.log("");
console.group(
`%c🚦 Render-Blocking Scripts (${blocking.length})`,
"color: #ef4444; font-weight: bold;"
);
blocking
.sort((a, b) => b.totalCostMs - a.totalCostMs)
.forEach((s) => {
const name = s.shortName.length > 40 ? s.shortName.slice(0, 37) + "..." : s.shortName;
console.log(
` 🔴 ${name}`
);
console.log(
` Download: ${formatMs(s.downloadDuration)} | Parse mob: ~${formatMs(s.estimatedParseMobile)} | Parse dsk: ~${formatMs(s.estimatedParseDesktop)} | Size: ${formatBytes(s.transferSize)}`
);
});
console.log("");
console.log(" These scripts block HTML parsing until fully downloaded and executed.");
console.log(" → Add defer to execute after parsing without blocking.");
console.log(" → Add async for independent scripts (analytics, ads).");
console.groupEnd();
}
// Code splitting opportunities
if (splitCandidates.length > 0) {
console.log("");
console.group(
`%c✂️ Code Splitting Candidates (${splitCandidates.length} scripts > 50KB decoded)`,
"color: #8b5cf6; font-weight: bold;"
);
splitCandidates
.sort((a, b) => b.decodedSize - a.decodedSize)
.forEach((s) => {
const name = s.shortName.length > 40 ? s.shortName.slice(0, 37) + "..." : s.shortName;
console.log(
` 📦 ${name}`
);
console.log(
` Decoded: ${formatBytes(s.decodedSize)} | Parse mob: ~${formatMs(s.estimatedParseMobile)} | Parse dsk: ~${formatMs(s.estimatedParseDesktop)} | Transfer: ${formatBytes(s.transferSize)}`
);
});
console.log("");
console.log(" Large scripts increase parse time on every page load, even if cached.");
console.log(" → Use dynamic import() to split at route or feature boundaries.");
console.log(" → Enable tree-shaking to eliminate unused exports.");
console.log(" → Consider module/nomodule pattern for modern browsers.");
console.groupEnd();
}
// Recommendations
const hasHighParseTime = totalEstParse > 500;
const hasBlockingScripts = blocking.length > 0;
const compressionRatio = totalDecodedSize / Math.max(totalSize, 1);
const poorCompression = compressionRatio < 2 && totalSize > 10 * 1024;
console.log("");
console.group("%c📝 Recommendations", "color: #3b82f6; font-weight: bold;");
if (!hasBlockingScripts && !hasHighParseTime && splitCandidates.length === 0) {
console.log(" %c✅ No critical JS issues found.", "color: #22c55e; font-weight: bold;");
}
if (hasBlockingScripts) {
console.log(` 🔴 ${blocking.length} render-blocking script(s) delay the page:`);
console.log(" → defer: safe for most scripts (runs after parsing, in order)");
console.log(" → async: for independent scripts with no DOM dependency");
}
if (hasHighParseTime) {
console.log(` ⚠️ High estimated parse cost (~${formatMs(totalEstParse)}):`);
console.log(" → Split bundles with dynamic import() at route boundaries");
console.log(" → Lazy-load non-critical features on demand");
console.log(" → Use module preload for critical chunks: <link rel='modulepreload'>");
}
if (poorCompression) {
console.log(` 📦 Low compression ratio (${compressionRatio.toFixed(1)}x) for ${formatBytes(totalSize)} transferred:`);
console.log(" → Enable Brotli compression on your server/CDN");
console.log(" → Ensure minification is enabled in your build");
}
console.log("");
console.log("%c 💡 For measured execution time (not estimates):", "color: #6b7280;");
console.log(
" Run the Long Animation Frames Script Attribution snippet to measure"
);
console.log(" actual execution time during user interactions.");
console.groupEnd();
console.groupEnd();
return {
script: "JS-Execution-Time-Breakdown",
status: "ok",
count: scripts.length,
details: {
blockingCount: blocking.length,
nonBlockingCount: nonBlocking.length,
totalTransferBytes: totalSize,
totalDecodedBytes: totalDecodedSize,
totalDownloadMs: Math.round(totalDownload),
totalEstParseMobileMs: Math.round(totalEstParse),
splitCandidatesCount: splitCandidates.length,
domInteractiveMs: Math.round(domInteractive),
domContentLoadedMs: Math.round(domContentLoaded),
loadEventMs: Math.round(loadEvent),
},
items: scripts.slice(0, 50).map(s => ({ shortName: s.shortName, isBlocking: s.isBlocking, downloadMs: Math.round(s.downloadDuration), estimatedParseMobileMs: s.estimatedParseMobile, transferBytes: s.transferSize, decodedBytes: s.decodedSize, corsRestricted: s.corsRestricted })),
issues: [
...(blocking.length > 0 ? [{ severity: "error", message: `${blocking.length} render-blocking script(s) delay HTML parsing` }] : []),
...(hasHighParseTime ? [{ severity: "warning", message: `High estimated parse cost (~${Math.round(totalEstParse)}ms on mobile)` }] : []),
...(splitCandidates.length > 0 ? [{ severity: "warning", message: `${splitCandidates.length} script(s) over 50KB decoded — consider code splitting` }] : []),
...(poorCompression ? [{ severity: "warning", message: `Low compression ratio (${compressionRatio.toFixed(1)}x) — enable Brotli/gzip` }] : []),
],
};
})();