If the page has got heavier and nobody says they’ve written new code, look at the script list. On corporate sites, most of the JavaScript load comes not from the site’s own code but from third-party scripts added over time: analytics, ad pixels, live chat, maps, video, A/B testing and heatmap tools. Each one is added by a different team, for a different reason. Nobody tracks the total cost. This guide describes the method we use in the field to measure JavaScript load, limit it with a performance budget and check the budget automatically in the release process.
What is a performance budget?
A performance budget is a written list of measurable limits that a page or template must not exceed. If it isn’t written down, it isn’t a budget. It can be defined in three types:
| Budget type | Example criterion | Strength |
|---|---|---|
| Quantity | Compressed JS size, number of third-party requests | Easy to measure, directly in the developer’s control |
| Scheduling | Total Blocking Time, LCP | Closer to the user experience |
| Rule | Passing Lighthouse audits | Catches specific mistakes (e.g. render-blocking resources) |
A byte limit alone isn’t enough for JavaScript. Of two scripts of the same size, one may run once and finish while the other constantly sets up timers and listeners. So define the size budget together with a timing measure (TBT in the lab, INP in the field). The numerical value is written per template after the current state has been measured and the target device class decided: the home page, a service page, a blog post and a form page have different needs.
Taking a script inventory
You can’t manage a budget without knowing what’s loaded. The first step is to take an inventory of all scripts loaded for each template. Alongside technical information, the inventory should include the business owner and the justification. A script whose owner can’t be found is a candidate for removal; in audits we see that this list is far from short.
| Script | Owner / purpose | Pages loaded on | Loading method |
|---|---|---|---|
| Analytics | Marketing / traffic measurement | All | async, consent-dependent |
| Live chat | Sales / pre-request | All | Synchronous (to be reviewed) |
| Map | Contact page | All (mistake) | Should be page-specific |
| Heatmap | Owner unclear | All | Removal candidate |
For the inventory, select the JS filter in the DevTools Network panel and add “Domain” to the columns to see which domains make requests. A negative filter such as -domain:example.com hides your own domain and leaves only third parties. To see a script loading other scripts (chaining), look at the “Initiator” column; tags loaded through Tag Manager appear here linked to the container script.
Measuring the cost of third-party scripts
A script’s cost isn’t just its download size: parsing, compiling, executing, the DOM elements it adds and the listeners it sets up all go on the bill. Use four practical methods together:
- Lighthouse’s third-party code finding (
third-party-summaryin older versions, the “3rd parties” insight in current versions): Lists third-party providers by transfer size and time spent on the main thread. - Coverage panel: Open the Coverage panel in DevTools and reload the page; for each file it shows the proportion of bytes downloaded but never executed during the recording. This ratio only reflects interactions made during the recording; code used later, such as menus or forms, can also appear “unused”. Large files with a high unused ratio should either be loaded per page or split.
- Comparison with request blocking: Right-click a request in the Network panel, block the domain and measure the page again. The difference between the two measurements is the script’s real cost to the page.
- Performance panel: Record with CPU throttling and group the Bottom-Up view with “Group by domain”; you can see which domains the main thread time goes to.
The third method ends the argument. Once the number is on the table, the script’s owner rethinks the justification too.
Loading strategies: async, defer, facade
If you can’t remove a script entirely, reduce its cost by changing when and how it loads.
async and defer
A <script src> tag without attributes stops HTML parsing. An async script is downloaded in parallel and runs as soon as it’s downloaded; there’s no ordering guarantee. defer scripts are downloaded in parallel and run in the order they’re written after HTML parsing finishes. type="module" scripts behave like defer by default. A mistake we often see in the field is giving async to one of two interdependent scripts: it usually works, sometimes the order breaks, and the error only appears on slow connections. In WordPress 6.3 and later, the loading strategy for theme and plugin scripts can be specified directly at registration:
wp_enqueue_script(
'corporate-ui',
get_template_directory_uri() . '/assets/js/ui.js',
array(),
'2.4.0',
array(
'strategy' => 'defer',
'in_footer' => true,
)
);
Load on interaction (facade)
A facade is the technique of replacing a heavy component with a lightweight placeholder that looks similar and loading the real script only when the user interacts. On corporate sites it works best for:
- Video embeds: A preview image and a play button instead of the video player; the iframe is added on click.
- Live chat: A static button in the bottom right; the chat script loads on click or after the user has been on the page for a certain time.
- Maps: A static map image and an “Open map” link on the contact page.
const chatButton = document.querySelector('#start-chat');
chatButton.addEventListener('click', () => {
const s = document.createElement('script');
s.src = 'https://chat.example-provider.com/widget.js';
s.async = true;
s.onload = () => window.ChatWidget?.open();
document.head.appendChild(s);
}, { once: true });
A facade has a price: the component opens with a delay on the first click. Signal this delay to the user with a loading indicator, and verify that the provider’s API supports opening programmatically (the open() call above varies by provider). If it doesn’t, the user has to click twice.
Tag Manager discipline
Tag managers such as Google Tag Manager move script additions outside the development process. Without discipline, this is where the budget gets broken fastest. Minimum rules:
- Each tag’s owner and purpose should be written in its name or notes; ownerless tags should be paused at the quarterly review.
- The “All pages” trigger should be given only to tags that are really needed on all pages; campaign pixels should be limited to the relevant landing pages.
- Custom HTML tags should go through code review; they’re the hardest tag type to audit, adding scripts directly to the page.
- Click and form triggers shouldn’t be defined more broadly than necessary; listeners that run on every click affect INP (for details see the INP improvement guide).
- Permission to publish the container should be limited and every publish should have a description; that way a performance regression can be matched to a publish.
- Marketing and analytics tags should load subject to user consent. A tag that doesn’t load before consent is both right under KVKK and costs nothing on first load; for implementation details, see the guide to loading tags based on cookie consent.
Checking the budget in CI
A written budget that isn’t checked automatically is exceeded within a few months. Lighthouse CI measures the specified URLs several times, compares the results with threshold values and fails the build if a threshold is exceeded. A sample lighthouserc.json:
{
"ci": {
"collect": {
"url": [
"https://staging.example.com/",
"https://staging.example.com/services/example-service/"
],
"numberOfRuns": 3
},
"assert": {
"assertions": {
"resource-summary:script:size": ["error", { "maxNumericValue": 300000 }],
"resource-summary:third-party:count": ["warn", { "maxNumericValue": 15 }],
"total-blocking-time": ["warn", { "maxNumericValue": 300 }]
}
},
"upload": {
"target": "filesystem",
"outputDir": "./lhci-report"
}
}
}
npm install -g @lhci/cli
lhci autorun
The thresholds in this example are only there to show the syntax; derive your own values from your current measurement. resource-summary sizes are in bytes and total-blocking-time in milliseconds. Since audit IDs can change between Lighthouse versions, verify that these IDs exist in the report JSON of the version you use. Lab measurements vary from run to run. Do several runs, keep timing measures at “warn” level at first and raise them to “error” after observing the noise. If you set “error” from day one, the team soon starts ignoring a pipeline that turns red at random. For the steps to add Lighthouse CI to the release pipeline, refer to the CI/CD with GitHub Actions guide.
Third-party scripts can change without a release of yours. So complement the CI check with periodic (e.g. daily) measurement against production and INP monitoring in the field. When the budget is exceeded, the rule is simple: a new script is added only if room is made in the budget (by removing another script or moving it to a facade). No exceptions.
For a tag architecture that balances measurement needs with performance cost, see our web analytics and conversion measurement service; for setting and monitoring the budget per template, see our web performance and Core Web Vitals service page. All Lighthouse CI configuration options are in the Lighthouse CI project documentation.
Frequently asked questions
What’s the right number for a performance budget?
There’s no universal number. First measure today’s JavaScript size and long task time per page template, then fix as the budget the level that meets Core Web Vitals thresholds on your target device class. What really matters is that the number is written down and that every new script is evaluated against it.
What’s the difference between async and defer?
Neither stops HTML parsing while the script downloads. An async script runs as soon as it’s downloaded, with no order guarantee; defer scripts run after HTML parsing finishes, in the order they’re written in the document and before the DOMContentLoaded event. Use defer for interdependent scripts and async for independent measurement scripts.
Does using Tag Manager slow down the page?
The container script itself is a cost, but the real load comes from the tags added to it. Ownerless custom HTML tags, marketing pixels fired on every page and click listeners affect both load time and INP. If the owner, purpose and trigger scope of tags are reviewed regularly, Tag Manager stays manageable.
Should Lighthouse CI be run against the production site?
The purpose of a budget check is to catch an overrun before release; so run it against a test environment or a preview environment spun up inside CI. Periodic runs against production are an additional check for noticing third-party scripts changing without a release of yours.