Scripts loading
Chrome 54+ Edge 17+ Firefox 61+ Safari 16.4+ Details
Analyzes all scripts on the page, showing their loading strategy and identifying potential performance issues. Scripts are often the biggest cause of main thread blocking and delayed rendering. This snippet helps you audit which scripts are blocking the critical rendering path.
Why this matters
Render-blocking scripts in <head> without async or defer pause HTML parsing while they download and execute. This delays First Contentful Paint (FCP) and Largest Contentful Paint (LCP). Even a single blocking script can add hundreds of milliseconds to your load time, especially on slower connections.
How scripts affect HTML parsing
Loading strategy comparison
| Strategy | Downloads | Executes | Blocks Parsing | Order Guaranteed |
|---|---|---|---|---|
| Normal | When encountered | Immediately | Yes | Yes |
| async | In parallel | When ready | Briefly | No |
| defer | In parallel | After parsing | No | Yes |
| module | In parallel | After parsing | No | Yes |
| async module | In parallel | When ready | Briefly | No |
When to use each
| Strategy | Best For |
|---|---|
| Normal | Critical scripts that must run before content (rare) |
| async | Independent scripts (analytics, ads) |
| defer | Scripts that need DOM, must run in order |
| module | Modern ES modules with imports |
Performance tip: Most scripts should use defer. Use async only for truly independent scripts that don’t rely on DOM or other scripts.
Snippet
Run it from an AI agent: the prompt uses the CLI to measure any URL and report the result.
// Script Loading Analysis
// https://webperf-snippets.net
(() => {
// @shared isOwnRequest
// True for the Resource Timing entry of a request a snippet made with ownRequestUrl: the page
// did not make it, so no snippet counts it.
function isOwnRequest(entry) {
return entry.name.endsWith("#webperf-snippets");
}
// @end-shared isOwnRequest
// Domains that belong to the site but differ from the page's root domain, such as its own CDN.
// They count as first party. Example, for a page on example.com whose CDN is example.net:
// const OWN_DOMAINS = ["example.net"];
const OWN_DOMAINS = [];
// @shared formatBytes
function formatBytes(bytes) {
if (bytes === null || bytes === undefined || Number.isNaN(bytes)) return "-";
if (bytes === 0) return "0 B";
const units = ["B", "KB", "MB", "GB"];
const i = Math.max(0, Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1));
return (bytes / Math.pow(1024, i)).toFixed(1) + " " + units[i];
}
// @end-shared formatBytes
const formatMs = (ms) => (ms > 0 ? ms.toFixed(0) + "ms" : "-");
// @shared getRootDomain
function getRootDomain(hostname) {
const host = hostname.replace(/\.$/, "");
// An IP address has no registrable domain, so each address is its own root
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(host) || host.includes(":")) return host;
const parts = host.split(".");
if (parts.length <= 2) return host;
// Country-code domains with a second-level suffix: example.co.uk, example.com.au, example.ac.jp
const secondLevelSuffixes = ["ac", "co", "com", "edu", "go", "gob", "gouv", "gov", "govt", "mil", "ne", "net", "nom", "or", "org", "sch"];
const tld = parts[parts.length - 1];
const sld = parts[parts.length - 2];
if (tld.length === 2 && secondLevelSuffixes.includes(sld)) return parts.slice(-3).join(".");
return parts.slice(-2).join(".");
}
// @end-shared getRootDomain
// @shared isFirstParty
function isFirstParty(hostname) {
const root = getRootDomain(hostname);
if (root === getRootDomain(location.hostname)) return true;
return OWN_DOMAINS.some((d) => getRootDomain(String(d).trim().toLowerCase().replace(/^[a-z]+:\/\//, "").split("/")[0]) === root);
}
// @end-shared isFirstParty
// @shared logOwnDomainsHint
function logOwnDomainsHint(thirdPartyCount) {
if (OWN_DOMAINS.length === 0 && thirdPartyCount > 0) {
console.log(
'%cℹ️ OWN_DOMAINS is empty. If this site serves its own assets from other domains (for example a CDN), add them at the top of the snippet, such as const OWN_DOMAINS = ["cdn.example.net"];, and run it again so they count as first party.',
"color: #3b82f6;"
);
}
}
// @end-shared logOwnDomainsHint
// A script whose URL cannot be parsed counts as first party
const isFirstPartyUrl = (url) => {
try {
return isFirstParty(new URL(url).hostname);
} catch {
return true;
}
};
// Get resource timing data
const resourceTiming = new Map();
performance.getEntriesByType("resource").filter((e) => !isOwnRequest(e)).forEach((r) => {
if (r.initiatorType === "script") {
resourceTiming.set(r.name, {
size: r.transferSize || 0,
// Cross-origin scripts without Timing-Allow-Origin report every size as 0
sizeKnown: r.transferSize > 0 || r.encodedBodySize > 0 || r.decodedBodySize > 0,
duration: r.duration,
startTime: r.startTime,
});
}
});
// Resource Timing does not expose the network priority, so it is estimated from the DOM with
// the rules of Chromium, checked against the priority it gives each request (CDP) in the e2e
// tests. Other browsers prioritize scripts in other ways.
const preloadedScripts = new Map();
document.querySelectorAll('link[rel~="preload"][as="script"], link[rel~="modulepreload"]').forEach((link) => {
const kind = link.relList.contains("modulepreload") ? "module" : "classic";
if (link.href) preloadedScripts.set(`${kind} ${link.href}`, link);
});
const firstBodyImage = document.body?.querySelector("img") ?? null;
const fetchPriorityOf = (el) => {
const value = (el.getAttribute("fetchpriority") || "").toLowerCase();
return value === "high" || value === "low" ? value : null;
};
// "high", "medium" or "low"; null for a script the browser never fetches
function estimatePriority(script, { isModule, isAsync, isDefer, isNoModule, inHead, preload }) {
if (isNoModule) return null;
// A preloaded script gets the priority of its preload, which fetched it first
if (preload) return fetchPriorityOf(preload) === "low" ? "low" : "high";
const hint = fetchPriorityOf(script);
if (isModule) return hint === "low" ? "low" : "high";
// async and defer, and scripts inserted by other scripts, which are async
if (isAsync || isDefer) return hint === "high" ? "high" : "low";
// A blocking script in <head> blocks rendering: fetchpriority="low" does not lower it
if (inHead) return "high";
if (hint) return hint;
// A blocking script after the first image of the body is a late one
const late = firstBodyImage && firstBodyImage.compareDocumentPosition(script) & Node.DOCUMENT_POSITION_FOLLOWING;
return late ? "medium" : "high";
}
// Analyze all external scripts
const externalScripts = Array.from(document.querySelectorAll("script[src]"));
// Analyze inline scripts
const inlineScripts = Array.from(document.querySelectorAll("script:not([src])"))
.filter((s) => s.innerHTML.trim().length > 0);
const scripts = externalScripts.map((script) => {
const src = script.src;
const timing = resourceTiming.get(src) || {};
const inHead = script.closest("head") !== null;
const isModule = script.type === "module";
const isAsync = script.async;
const isDefer = script.defer;
// A nomodule script is the fallback for browsers without ES modules. A browser that has the
// noModule property supports modules, so it never fetches or runs the script: it blocks nothing.
const isNoModule = script.noModule === true && !isModule;
// Determine loading strategy
let strategy = "blocking";
if (isNoModule) {
strategy = "nomodule";
} else if (isModule && isAsync) {
strategy = "async module";
} else if (isModule) {
strategy = "module";
} else if (isAsync) {
strategy = "async";
} else if (isDefer) {
strategy = "defer";
}
const isBlocking = strategy === "blocking";
const firstParty = isFirstPartyUrl(src);
const preload = preloadedScripts.get(`${isModule ? "module" : "classic"} ${src}`) ?? null;
return {
src,
shortSrc: src.split("/").pop()?.split("?")[0] || src,
strategy,
isBlocking,
inHead,
firstParty,
isModule,
isAsync,
isDefer,
fetchpriority: fetchPriorityOf(script),
priority: estimatePriority(script, { isModule, isAsync, isDefer, isNoModule, inHead, preload }),
preloaded: preload !== null,
size: timing.size || 0,
// A nomodule script is never fetched, so its size is known: nothing was downloaded
sizeKnown: !!timing.sizeKnown || (isNoModule && !resourceTiming.has(src)),
duration: timing.duration || 0,
startTime: timing.startTime || 0,
element: script,
};
});
// Categorize scripts
const blocking = scripts.filter((s) => s.isBlocking);
const blockingInHead = blocking.filter((s) => s.inHead);
const asyncScripts = scripts.filter((s) => s.strategy === "async" || s.strategy === "async module");
const deferScripts = scripts.filter((s) => s.strategy === "defer");
const moduleScripts = scripts.filter((s) => s.strategy === "module");
const noModuleScripts = scripts.filter((s) => s.strategy === "nomodule");
const thirdPartyBlocking = blocking.filter((s) => !s.firstParty);
// Calculate totals
const totalSize = scripts.reduce((sum, s) => sum + s.size, 0);
const blockingSize = blocking.reduce((sum, s) => sum + s.size, 0);
// Rating
let rating, ratingColor;
if (blockingInHead.length === 0) {
rating = "Good";
ratingColor = "#22c55e";
} else if (blockingInHead.length <= 2 && thirdPartyBlocking.length === 0) {
rating = "Needs Review";
ratingColor = "#f59e0b";
} else {
rating = "Needs Optimization";
ratingColor = "#ef4444";
}
// Display results
console.group("%c📜 Script Loading Analysis", "font-weight: bold; font-size: 14px;");
// Summary
console.log("");
console.log("%cSummary:", "font-weight: bold;");
console.log(` Total external scripts: ${scripts.length}`);
console.log(` Total inline scripts: ${inlineScripts.length}`);
console.log(` Total size: ${formatBytes(totalSize)}`);
console.log("");
console.log("%cBy loading strategy:", "font-weight: bold;");
console.log(` 🔴 Blocking: ${blocking.length} (${formatBytes(blockingSize)})`);
console.log(` ⚡ Async: ${asyncScripts.length}`);
console.log(` 📋 Defer: ${deferScripts.length}`);
console.log(` 📦 Module: ${moduleScripts.length}`);
if (noModuleScripts.length > 0) {
console.log(` ⏭️ nomodule (not fetched by this browser): ${noModuleScripts.length}`);
}
console.log("");
console.log(` Rating: %c${rating}`, `color: ${ratingColor}; font-weight: bold;`);
// Detailed table
if (scripts.length > 0) {
console.log("");
console.group("%c📊 External Scripts", "color: #3b82f6; font-weight: bold;");
const tableData = scripts
.sort((a, b) => {
// Sort: blocking first, then by size
if (a.isBlocking !== b.isBlocking) return a.isBlocking ? -1 : 1;
return b.size - a.size;
})
.map((s) => ({
Script: s.shortSrc,
Strategy: s.isBlocking ? `🔴 ${s.strategy}` : s.strategy,
Location: s.inHead ? "head" : "body",
Party: s.firstParty ? "1st" : "3rd",
Priority: s.priority ?? "-",
Size: formatBytes(s.size),
Duration: formatMs(s.duration),
}));
console.table(tableData);
// Elements for inspection
console.log("");
console.log("%c🔎 Elements (sorted by load order):", "font-weight: bold;");
scripts
.sort((a, b) => a.startTime - b.startTime)
.forEach((s, i) => {
const marker = s.isBlocking ? "🔴" : "✅";
console.log(`${i + 1}. ${marker} ${s.shortSrc}`, s.element);
});
console.groupEnd();
}
// Issues
const issues = [];
if (blockingInHead.length > 0) {
issues.push({
code: "blocking-in-head",
severity: "error",
message: `${blockingInHead.length} blocking script(s) in <head>`,
scripts: blockingInHead,
fix: "Add 'defer' or 'async' attribute, or move to end of <body>",
});
}
if (thirdPartyBlocking.length > 0) {
issues.push({
code: "third-party-blocking",
severity: "error",
message: `${thirdPartyBlocking.length} third-party blocking script(s)`,
scripts: thirdPartyBlocking,
fix: "Add 'async' for independent scripts, or load dynamically",
});
}
const largeBlocking = blocking.filter((s) => s.size > 50 * 1024);
if (largeBlocking.length > 0) {
issues.push({
code: "large-blocking",
severity: "warning",
message: `${largeBlocking.length} large blocking script(s) (> 50 KB)`,
scripts: largeBlocking,
fix: "Split code, use defer, or lazy load",
});
}
// Scripts that could be deferred
const couldDefer = blocking.filter((s) => {
// Scripts at end of body could likely use defer
const bodyScripts = Array.from(document.body.querySelectorAll("script[src]"));
const isLastInBody = bodyScripts.indexOf(s.element) >= bodyScripts.length - 3;
return isLastInBody;
});
if (couldDefer.length > 0 && blocking.length > couldDefer.length) {
issues.push({
code: "could-defer",
severity: "info",
message: `${blocking.length - couldDefer.length} blocking script(s) could potentially use defer`,
fix: "Test with defer attribute to improve parsing performance",
});
}
// Third-party scripts that do not block the parser but compete for the network at high
// priority with the CSS, the fonts and the LCP image. One issue per script.
scripts
.filter((s) => !s.firstParty && s.isModule && s.priority === "high")
.forEach((s) => {
issues.push({
code: "third-party-module-high-priority",
severity: "warning",
message: s.preloaded
? `${s.shortSrc}: third-party module script preloaded at high priority; add fetchpriority="low" to its <link rel="modulepreload">, unless the page needs it to render`
: `${s.shortSrc}: third-party module script fetched at high priority, like a blocking script; add fetchpriority="low" unless the page needs it to render`,
target: s.src,
scripts: [s],
fix: s.preloaded
? 'Add fetchpriority="low" to the <link rel="modulepreload">, which sets the priority of the module'
: 'Add fetchpriority="low" to the <script type="module">',
});
});
scripts
.filter((s) => !s.firstParty && !s.isModule && (s.isAsync || s.isDefer) && s.fetchpriority === "high")
.forEach((s) => {
issues.push({
code: "non-blocking-high-priority",
severity: "warning",
message: `${s.shortSrc}: fetchpriority="high" raises a third-party ${s.isAsync ? "async" : "defer"} script to high priority; remove it so the script loads at low`,
target: s.src,
scripts: [s],
fix: 'Remove fetchpriority="high"',
});
});
if (issues.length > 0) {
console.log("");
console.group("%c⚠️ Issues Found", "color: #ef4444; font-weight: bold;");
issues.forEach((issue) => {
const icon = issue.severity === "error" ? "🔴" : issue.severity === "warning" ? "🟡" : "💡";
console.log("");
console.log(`%c${icon} ${issue.message}`, "font-weight: bold;");
if (issue.scripts) {
issue.scripts.forEach((s) => {
console.log(` • ${s.shortSrc} (${formatBytes(s.size)})`);
});
}
console.log(` → ${issue.fix}`);
});
console.groupEnd();
} else if (scripts.length > 0) {
console.log("");
console.log(
"%c✅ All scripts use non-blocking loading strategies!",
"color: #22c55e; font-weight: bold;"
);
}
// First/Third party breakdown
const firstPartyScripts = scripts.filter((s) => s.firstParty);
const thirdPartyScripts = scripts.filter((s) => !s.firstParty);
if (thirdPartyScripts.length > 0) {
console.log("");
console.group("%c🌐 Third-Party Scripts", "color: #8b5cf6; font-weight: bold;");
const thirdPartyByHost = new Map();
thirdPartyScripts.forEach((s) => {
try {
const host = new URL(s.src).hostname;
if (!thirdPartyByHost.has(host)) {
thirdPartyByHost.set(host, { count: 0, size: 0, blocking: 0 });
}
const data = thirdPartyByHost.get(host);
data.count++;
data.size += s.size;
if (s.isBlocking) data.blocking++;
} catch {}
});
const hostTable = Array.from(thirdPartyByHost.entries())
.sort((a, b) => b[1].size - a[1].size)
.map(([host, data]) => ({
Host: host,
Scripts: data.count,
Size: formatBytes(data.size),
Blocking: data.blocking > 0 ? `⚠️ ${data.blocking}` : "0",
}));
console.table(hostTable);
console.groupEnd();
}
// Best practices (defined here so firstPartyScripts/thirdPartyScripts are in scope for return)
console.log("");
console.group("%c📝 Best Practices", "color: #3b82f6; font-weight: bold;");
console.log("");
console.log("%cFor most scripts (need DOM, run in order):", "font-weight: bold;");
console.log('%c<script src="app.js" defer></script>', "font-family: monospace; color: #22c55e;");
console.log("");
console.log("%cFor independent scripts (analytics, ads):", "font-weight: bold;");
console.log('%c<script src="analytics.js" async></script>', "font-family: monospace; color: #22c55e;");
console.log("");
console.log("%cFor ES modules:", "font-weight: bold;");
console.log('%c<script type="module" src="app.mjs"></script>', "font-family: monospace; color: #22c55e;");
console.log("");
console.log("%cFor critical inline scripts:", "font-weight: bold;");
console.log('%c<script>/* Only essential initialization */</script>', "font-family: monospace; color: #22c55e;");
logOwnDomainsHint(thirdPartyScripts.length);
console.groupEnd();
console.groupEnd();
const agentRating = blockingInHead.length === 0 ? "good" :
(blockingInHead.length <= 2 && thirdPartyBlocking.length === 0) ? "needs-improvement" : "poor";
// The list keeps the 50 scripts to look at first (render-blocking, then third-party, then the largest); count and details cover all of them
const MAX_ITEMS = 50;
const rankedScripts = [...scripts].sort(
(a, b) =>
Number(b.isBlocking) - Number(a.isBlocking) ||
Number(a.firstParty) - Number(b.firstParty) ||
b.size - a.size
);
return {
script: "Script-Loading",
status: "ok",
count: scripts.length,
corsLimitedAnalysis: scripts.some((s) => !s.sizeKnown),
rating: agentRating,
details: {
totalSizeBytes: totalSize,
sizeUnknownCount: scripts.filter((s) => !s.sizeKnown).length,
byStrategy: {
blocking: blocking.length,
async: asyncScripts.length,
defer: deferScripts.length,
module: moduleScripts.length,
nomodule: noModuleScripts.length,
},
byParty: {
firstParty: firstPartyScripts.length,
thirdParty: thirdPartyScripts.length,
},
thirdPartyBlockingCount: thirdPartyBlocking.length,
priorityModel: "chromium",
byPriority: {
high: scripts.filter((s) => s.priority === "high").length,
medium: scripts.filter((s) => s.priority === "medium").length,
low: scripts.filter((s) => s.priority === "low").length,
},
},
items: rankedScripts.slice(0, MAX_ITEMS).map((s) => ({
url: s.src,
shortName: s.shortSrc,
strategy: s.strategy,
location: s.inHead ? "head" : "body",
party: s.firstParty ? "first" : "third",
priority: s.priority,
fetchpriority: s.fetchpriority,
sizeBytes: s.size,
sizeKnown: s.sizeKnown,
durationMs: Math.round(s.duration),
})),
issues: [
...issues.map((i) => ({ code: i.code, severity: i.severity, message: i.message, ...(i.target ? { target: i.target } : {}) })),
...(scripts.some((s) => !s.sizeKnown)
? [{ code: "unknown-size", severity: "info", message: `${scripts.filter((s) => !s.sizeKnown).length} script(s) have an unknown size (missing Timing-Allow-Origin); total size is a lower bound` }]
: []),
],
};
})();
Understanding the results
Summary section
- Total scripts (external and inline)
- Breakdown by loading strategy
- Overall rating based on blocking scripts
Loading strategy legend
- Blocking: Stops HTML parsing (bad for performance)
- Async: Downloads in parallel, executes when ready
- Defer: Downloads in parallel, executes after parsing
- Module: ES module, which runs after parsing like
deferbut downloads at high priority - nomodule: Fallback for browsers without ES modules, such as the polyfills script that frameworks inject. Browsers that support modules never download or run it, so it is not counted as blocking
Details table
| Column | Description |
|---|---|
| Script | Filename |
| Strategy | Loading strategy with indicator |
| Location | head or body |
| Party | 1st (your domain) or 3rd (external) |
| Priority | Network priority Chrome gives the script: high, medium or low (priority in the returned items, with the fetchpriority attribute beside it) |
| Size | Transfer size; unknown for cross-origin scripts without Timing-Allow-Origin (sizeKnown: false in the returned items) |
| Duration | Time to load |
Issues detected
| Issue | Severity | Why It Matters |
|---|---|---|
Blocking scripts in <head> | Error | Delays all content below |
| Third-party blocking scripts | Error | External dependency blocking your page |
| Large blocking scripts (> 50 KB) | Warning | Significant parsing delay |
| Third-party module at high priority | Warning | Competes for the network with CSS, fonts and the LCP image without blocking anything |
fetchpriority="high" on a third-party async or defer script | Warning | Raises a script that loads at low priority to high |
Network priority
A script that does not block parsing can still download at high priority and compete for the network with the CSS, the fonts and the LCP image. Resource Timing does not expose the priority, so the snippet estimates the one Chrome gives each script from its attributes and its place in the document (details.priorityModel: "chromium"). Firefox and Safari prioritize scripts in other ways.
| Script | Without fetchpriority | fetchpriority="low" | fetchpriority="high" |
|---|---|---|---|
Blocking, in <head> | High | High (no effect) | High |
Blocking, in <body> before any image | High | Low | High |
Blocking, in <body> after an image | Medium | Low | High |
async or defer | Low | Low (no effect) | High |
| Inserted by another script | Low | Low (no effect) | High |
type="module", async or not | High | Low | High |
Preloaded with rel="preload" or rel="modulepreload" | The priority of the preload |
A third-party module is the case to look for: it does not block parsing, so it looks fine by its strategy, yet it downloads at high priority. fetchpriority="low" lowers it. On an async or defer script, fetchpriority="low" changes nothing in Chrome, since it already loads at low priority, and the snippet does not flag it.
<!-- ❌ A third-party widget that downloads at high priority -->
<script type="module" src="https://widgets.example.net/chat.mjs"></script>
<!-- ✅ The same widget at low priority -->
<script type="module" src="https://widgets.example.net/chat.mjs" fetchpriority="low"></script>An end-to-end test of the CLI checks every row of this table against the priority Chrome gives each request, read with the Chrome DevTools Protocol, so the estimate follows Chrome when it changes.
Script loading decision tree
Scripts are first party when they share the page’s root domain. A site’s own CDN on a different root domain, such as example.net for example.com, is reported as a third party, because the comparison uses root domains only. To count those domains as first party, list them in the OWN_DOMAINS constant at the top of the snippet, for example const OWN_DOMAINS = ["example.net"];. Entries can be domains or URLs. While the list is empty and the page has third-party resources, the snippet prints a reminder in the console.
Browser support
The snippet needs these features to run.
| Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| PerformanceResourceTiming.transferSize (opens in a new tab) | 54 | 17 | 45 | 16.4 |
| Document.scripts (opens in a new tab) | 36 | 12 | 61 | 10.1 |
| All of the above | 54 | 17 | 61 | 16.4 |
The snippet reports on these features. It runs without them, and its advice on each one applies where it is supported.
| Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| <script fetchpriority> (opens in a new tab) | 101 | 101 | 132 | 17.2 |
Source: MDN browser compatibility data (opens in a new tab), version 8.1.10.
Further reading
- Efficiently load third-party JavaScript (opens in a new tab) | web.dev
- Eliminate render-blocking resources (opens in a new tab) | Chrome Developers
- JavaScript modules (opens in a new tab) | MDN
- async vs defer (opens in a new tab) | javascript.info