
How to Audit Third-Party Scripts: What One Tag Costs
Somebody always adds one more tag, so we built our own bad vendor to see the damage of one, a script which the server delivers after 1 second and which then eats the processor 400 ms doing nothing useful.
First we put it in head like the vendor's instructions love, just script src, and the first paint arrived at 1488 ms, one and half second of white screen for a thing which draws zero pixels! With defer the paint was at 52 ms, with async at 72, same file, one word of difference, we were quite happy, until we looked at long tasks, 400 ms was there in all three, so we only moved the pain, the main thread is frozen when the script runs and whoever clicks in this moment is waiting, the only cure for this part is not running it at all or running it when somebody asks, like chat widget which is a dumb button until the click.
<script src="https://cdn.example-analytics.com/a.js" defer></script>
const button = document.querySelector('#chat-button');
button.addEventListener('click', () => {
button.disabled = true;
const script = document.createElement('script');
script.src = 'https://widget.example-chat.com/loader.js';
script.onload = () => window.ExampleChat.open();
script.onerror = () => { button.disabled = false; };
document.head.append(script);
});
So when we audit a site we paste the console snippet from below which lists foreign scripts, we block them in DevTools one by one and watch the page, and then the uncomfortable question in the team, who opened the data of this tool last month, silence means delete. Our vendor here is invented and runs on localhost, real ones pull more files after the first, your list will be longer than your HTML shows.
const rows = performance.getEntriesByType('resource')
.filter((r) => new URL(r.name).hostname !== location.hostname)
.map((r) => ({
host: new URL(r.name).hostname,
type: r.initiatorType,
kb: Math.round(r.transferSize / 1024),
}));
console.table(rows);
See also: your performance budget and INP and long tasks.