Why third-party JavaScript is a separate risk category
Third-party code can open extra connections, load dependencies, create iframes, install listeners and timers, and consume main-thread time. Its cost can also change when the provider updates the script. [2]
HTTP Archive Web Almanac 2025 found third parties on at least 90% of analyzed pages; across all sites the median was 83 third-party requests on desktop and 79 on mobile. [12]
Start with an inventory: what the page really loads
Build an inventory of vendors, origins, owners, purpose and loading phase. One tag can load several additional vendors, so inspect the real dependency chain.
async and defer: do not block the parser unnecessarily
<script async src="https://example.com/analytics.js"></script>
<script defer src="https://example.com/widget.js"></script>
Classic scripts without async, defer or modules can block parsing. async is suitable for independent scripts; defer preserves order and waits until parsing finishes. [7][8]
Do not load everything on the first screen
Delay non-critical analytics extensions, chat, maps, ads below the fold and marketing tools until they are likely to be needed. [1][2]
iframes and loading="lazy": a simple embed optimization
<iframe src="https://video.example/embed/123" loading="lazy"></iframe>
An iframe is another document with its own subresources. loading="lazy" can avoid downloading offscreen embeds during initial load. [4]
Facades and click-to-load: do not fetch a widget before intent
A facade is a lightweight placeholder that becomes the real embed only after interaction. This is especially effective for videos, chat, maps and social widgets. [3]
Chat widgets should react to intent, not pageview
Many chat products download UI code, assets and tracking before the user opens the widget. Prefer a lightweight first-party launcher and initialize the vendor after intent.
A Tag Manager does not remove performance responsibility
Tag managers simplify deployment but do not reduce the network or CPU cost of tags. Review tags regularly and remove obsolete rules.
Consent: do not start the whole stack at once
After consent, prioritize tags instead of firing the entire marketing stack at once. Permission and scheduling are separate concerns.
Ads: network, CPU and layout stability cost
Ads may involve multiple origins, auctions, measurement code and iframes. Reserve slot dimensions and lazy-load offscreen inventory where supported.
Partytown and Web Workers: moving selected third-party work off the main thread
<script type="text/partytown" src="https://example.com/tracker.js"></script>
Partytown can run selected third-party scripts in a Web Worker. Popular services are documented, but compatibility is not universal. [9][10][11]
Partytown reached stable v1.0.0 on October 10, 2026 and v1.0.2 on October 22, but vendor compatibility still requires testing. [14]
DevTools: separate first-party from third-party
Chrome DevTools can distinguish first- and third-party events and report transfer and main-thread time by entity. [5][6]
Measure real user flows, not only initial load
Record complete journeys such as opening chat, playing video, checkout and SPA navigation. Initial-load traces alone can miss the expensive phase.
Use a dedicated third-party budget
Keep a dedicated budget for third-party bytes, origins and CPU so marketing additions cannot bypass application performance targets.
A practical third-party JavaScript optimization workflow
Inventory vendors, remove low-value tools, fix loading strategy, lazy-load embeds, profile again and only then consider advanced off-main-thread techniques.
