We can optimize the code written by us to improve the performance score to some extent. However, apart from our code websites usually use third-party scripts too. Third-party scripts are the code embedded in our website and not directly under our control. These third-party scripts are added to perform a specific task like gathering and analyzing data of the visitors our website receives to get useful insights, ad tracking and tracking user behaviour and interaction. We tend to use these scripts on our website as they are battle-tested to avoid re-inventing the wheel and focus efforts on creating value for our visitors.
JS in a browser is a single-threaded language, if the third-party scripts which we use are not optimized and are large, they tend to consume a large chunk of the main thread’s precious resources. These scripts cannot be optimized for speed since they are beyond our control. There are a few tricks to reduce their upfront damaging effects, like waiting until after the page load to run these scripts, regardless, they’re still running hundreds of kilobytes (and commonly, even a few megabytes) of JavaScript on the user’s main thread. This can have a grave impact on the experience for users browsing our website on low-end mobile devices having limited resources.
Partytown is a small library (around 6 Kb) aimed at freeing the main thread off resource-intensive scripts and offloading them to a web worker. Its goal is to free main thread resources so that it can dedicatedly run code related to our business logic, sandbox and isolate third-party scripts and allow or deny their access to main thread APIs.

Partytown utilizes web workers and service workers to achieve its goals.
You can read more about Web Workers and Service Workers on MDN web docs.
TL;DR
Web workers allow running code on background threads, they can communicate asynchronously with the main thread and do not have access to DOM.

Service workers are specialized workers and they act as a proxy between the browser and network and can intercept network requests. They too can communicate asynchronously with the main thread.


If this piques your interest, you can learn more about Partytown from below resources: