Skip to content

Search

Articles, tools, solutions and knowledge base guides

Start typing to search across articles, tools and apps.

Third-party JavaScript without the performance hit: analytics, chat, ads and widgets

A site can be carefully optimized and still become slow after adding analytics, tag managers, chat, ads, A/B testing, maps, videos and other external services. Third-party code competes with your own application for network bandwidth, CPU and main-thread time, and its behavior can change without a deployment on your side. These integrations therefore need explicit budgets, loading strategies and regular audits.

Published Written by Reading time 26 min read
On this page Contents: quick navigation between sections

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.

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.

Treat third-party JavaScript as part of the product, not as code outside the performance budget. Keep only integrations with clear value, load them at the right time, use lazy loading and facades for embeds, control the number of origins and tags, and measure actual transfer and main-thread cost in DevTools. Worker-based tools such as Partytown can help in selected cases, but they do not replace an audit. The cheapest third-party script is the one you do not run.

Tools for this topic

Third-Party JavaScript Web Performance Analytics Chat Widgets Advertising Tag Manager Lazy Loading Facades Core Web Vitals INP DevTools Partytown

Frequently asked questions

What is third-party JavaScript?

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]

Does async remove script cost?

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]

Can iframes be lazy-loaded?

An iframe is another document with its own subresources. loading="lazy" can avoid downloading offscreen embeds during initial load. [4]

What is a facade?

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]

Does a tag manager make tags free?

Tag managers simplify deployment but do not reduce the network or CPU cost of tags. Review tags regularly and remove obsolete rules.

Can Partytown run every third-party script?

Partytown can run selected third-party scripts in a Web Worker. Popular services are documented, but compatibility is not universal. [9][10][11]

How do I find expensive third-party code?

Chrome DevTools can distinguish first- and third-party events and report transfer and main-thread time by entity. [5][6]

Should all tags fire immediately after consent?

After consent, prioritize tags instead of firing the entire marketing stack at once. Permission and scheduling are separate concerns.

Sources and references

  1. web.dev, Efficiently load third-party JavaScript↩1↩2↩3
  2. web.dev, Load third-party JavaScript↩1↩2↩3
  3. web.dev, Best practices for using third-party embeds↩1↩2↩3
  4. web.dev, Lazy load images and iframe elements↩1↩2↩3
  5. Chrome DevTools, Performance features reference↩1↩2↩3
  6. Chrome Performance Insights, 3rd parties↩1↩2↩3
  7. MDN, script element↩1↩2↩3
  8. MDN, HTMLScriptElement.defer↩1↩2
  9. Partytown, Introduction↩1↩2↩3
  10. Partytown, Common Services↩1↩2↩3
  11. Partytown, FAQ↩1↩2↩3
  12. HTTP Archive Web Almanac 2025, Third Parties↩1↩2
  13. HTTP Archive Web Almanac 2025, Page Weightfurther reading
  14. Partytown releases, v1.0.0 and v1.0.2↩1↩2

Was this helpful?

Get new articles by email

One short email per new Learning article. No spam, unsubscribe in one click.

We only use your email to send new articles. No third-party sharing.

Back to Learning