First, what is Partytown? It's a JS library that lets you offload 3rd party scripts to a web worker to remove the negative performance impact they can have on your site
How do you integrate Partytown? You can easily add it to any site with a small snippet, and then tag any scripts that you want to load in the Web Worker with type="text/partytown" partytown.builder.io/html
Any scripts that you add type="text/partytown" will load by default in a Web Worker, but have full access to global variables
We use JS Proxies to supply main thread globals to the worker thread, by intercepting them and forwarding to the main thread
And here is the magic part: we use a *synchronous* xhr request to block the worker thread to retrieve the needed value from the main thread
And now we can use a Service Worker to intercept the /proxytown request, and async postMessage to the main thread to get the needed value, and return it
And that's it! The end result: you can now seamlessly drop in a whole range of third party scripts to run in a web worker, removing the main thread perf cost of them
Bonus - if you are wondering how Partytown handles objects or functions as return values, we can’t send the actual ref back, send back a proxy object pointing to it. When methods are accessed or called, we invoke them on the original and send the value back
Bonus pt 2: a better look at what happens in each thread
LLMs are bad at acknowledging what they don't know
But they are good at answering questions from a well defined context
So instead, you can provide a set of information via RAG (retrieve it and supply it in the prompt) and tell the LLM to *only* answer questions from this data
Don't be that teammate who blindly refactors code and only makes things worse.
Let's look at some good vs bad refactoring patterns with real examples 🧵
Let's take this code.
I hired a developer once that saw us calling this `functions.runWith(...)` repeatedly with different options and decided to consolidate it all into one createApi function
This new, consolidated code, had a huge issue.
Can you see it?
When we started deploying these APIs, they began breaking left and right.
In all seriousness though, I know I post a lot of coding tips and "do this not that" best practices stuff, but I want to use this as a reminder to point out...
I don't write perfect code. No one does. My code has been complained about by fellow engineers as much as anyone, if not probably more.