DEV Community

Cover image for Introducing Partytown πŸŽ‰: Run Third-Party Scripts From a Web Worker

Introducing Partytown πŸŽ‰: Run Third-Party Scripts From a Web Worker

Adam Bradley on September 23, 2021

A fun location for your third-party scripts to hang out Performance is always top of mind for any website or web app. It’s of no surprise that a...
Collapse
 
mappmechanic profile image
Rahat Khanna

This is such an intriguing, simple and yet so innovative idea. Thanks for doing this and sharing it with the community all along. I have always loved your work at Ionic and looking forward how you innovate at Builder.io

Collapse
 
vaibhavrajsingh2001 profile image
Vaibhav Raj Singh

Love this idea! It's high time we aandboxed those pesky scripts.

Collapse
 
cyrstron profile image
cyrstron • Edited

But where are the actual numbers? What performance boost did you exactly get with this approach?

Isn't DOM accessing and modifying itself is the most expensive part? Does profit of moving everything else except that to a web-worker really worth it?

Also moving data from the main thread to a web-worker and back takes resources as well (for instance, serialization/deserialization).

Collapse
 
steve8708 profile image
Steve Sewell

We've seen a single third party script for analytics drop a lighthouse score by 20 points. Analytics in particular is a high priority use case as it does not need constant read/write with the main thread (aka it's not needing to constantly serialize/dserialize), it just needs to not throw errors if it tries to run something like if (window.something) { ... }.

With projects like Qwik, Astro, Marko, React server components, everyone is trying to remove (or even eliminate) needing to run JS on the main thread to initialize a site or app, but what we immediately find is your hard work is quickly negated by the amount of 3rd party services that eat the main threads resources that are background oriented anyway (e.g. analytics). Partytown aims to solve for this use case by moving those expensive scripts to the worker and free up the main thread for just your code

Our next step is to gather production data and performance impact of running 3rd party scripts (on real websites) vs not (in partytown or eliminating entirely) to quantify the impact of these scripts more. Will share when we have the data available, so stay tuned :)

Collapse
 
cyrstron profile image
cyrstron

Sounds great!

Are there only specific use cases for this approach or it may be useful for most of the third party libs?

Thread Thread
 
steve8708 profile image
Steve Sewell

Absolutely - right now we’re mostly focused on analytics like google tag manager, google analytics, conversion tracking JS, etc.

Collapse
 
adambullmer profile image
Adam Bullmer

Love this idea! Haven't thought of a polyfill to run scripts like amplitude, branch, datadog, or segment off the main thread. Makes total sense, and would be awesome for wider support for ancillary eventing libraries to clear their queues after a user has bounced from your site.

Collapse
 
132 profile image
Yisar

github.com/yisar/fard/tree/master/...
A long time ago, I used a similar method to run fre to the worker, but it only has 1KB and does not need to simulate dom.

Collapse
 
essentialrandom profile image
Essential Randomness

Very curious to see where this goes. There's definitely some things I'd love to take off the main thread, and this seems like a good non-destructive way of achieving it.

Collapse
 
aspiiire profile image
Aspiiire

This is a fantastic awesome idea really love it! I cannot wait to implement it

Collapse
 
sirseanofloxley profile image
Sean Allin Newell

I like this A LOT A LOT!! πŸŽ‰πŸŽŠπŸŽ‰πŸ’ƒπŸ’ƒπŸŽŠπŸŽ‰πŸ₯³

Collapse
 
edjevw12 profile image
Edwinvw

Love it!

Collapse
 
souksyp profile image
Souk Syp.

Wow

Collapse
 
fili profile image
Fili

Moving third party scripts off the main thread to a service worker is very progressive! Love the concept.

Collapse
 
soniar4i profile image
Sonia C.

Hey Adam!

Great article I was wondering if this will allow script tags to be easily added or removed from dom tree. I’m currently working with an SPA and handling those third party ser a nightmare since you can’t control what gets injected.

Thanks!

Collapse
 
camaross profile image
Denis

I wonder how much better is this compared to delayed script injection? What I did before for the same purpose was pushing those 3rd party into the DOM, say, 1 sec after window's "load" event. That way, I saw no negative performance impact and the only downside was that a hit would not be captured by analytics if the user leaves the page too soon. Nevertheless, great technique!

Collapse
 
icantunderstand profile image
icantunderstand

great idea,hopelly it will soon work on production

Collapse
 
niyazafazl profile image
niyazafazl

Partytown can use with the other third party libraries like moment, single-spa for React.
How can we set the forward config for those libraries? Can please share the sample code for that?

The below is the Partytown forward config for GTM, I want to set the same for toehr libraries.

import { Partytown } from '@builder.io/partytown/react';

export function Head() {
return (
<>

</>
);
}

Collapse
 
coderocker profile image
Hrishikesh

Awesome Idea!
Love to learn more about this.

Collapse
 
saucyofficiale profile image
Saucy

Would this work for jquery? I want to render jquery library and not let it block page render. Defer tag does not work.

Collapse
 
adamdbradley profile image
Adam Bradley

Yes it should be able to work, however, the goals of Partytown may not be the same of why you'd like jquery to run in the web worker. If rendering your webpage is dependent on jquery working immediately, then Partytown may not be the best for this. However, if jquery is only handling click events, then it could be worth experimenting with. Please test it out and let us know how it goes on the the repo issues! Thanks

Collapse
 
tushar0704 profile image
Tushar Agarwal

Love this idea! But how do I run jQuery functions for gtag using window.addEventListener on partytown?

Collapse
 
xblabs profile image
Orwell's Ghost

Will this be worth or even working for banner ads as well?