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.
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.
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: