Steve Sewell Profile picture
Apr 25 10 tweets 3 min read
How Partytown works thread: 🧵 👇 Overview of how Partytown works
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 Diff of code to add Partytown
Any scripts that you add type="text/partytown" will load by default in a Web Worker, but have full access to global variables Code example of what runs in the worker vs main thread
We use JS Proxies to supply main thread globals to the worker thread, by intercepting them and forwarding to the main thread Visual of intercepting main thread variables
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 Visual of a synchronous xhr
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 Visual of how the service worker intercepts the request to g
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

You can learn more about Partytown here: github.com/builderio/part…
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

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Steve Sewell

Steve Sewell Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Steve8708

Mar 30
Perf quiz!

What is the correct attribute for browser-native lazy loading?

Answer in 🧵👇
The answer is 4! Did you get it right?

Learn more: web.dev/browser-level-…
PS - this attributes is now available in all major browsers!
Read 5 tweets
Mar 29
💡 Web performance tip

Optimizing your largest contentful paint (LCP) can be hard

Here are 8 tips, with code snippets and visuals, to help your pages load fast for optimal SEO and UX

🧵👇 Image
First, how is LCP measured?

> The Largest Contentful Paint (LCP) metric reports the render time of the largest image or text block visible within the viewport, relative to when the page first started loading

web.dev/lcp/ Image
As much as possible, serve all pages and content pregenerated from a CDN cache. This ensures that response times are fast, and content can be visible as quickly as possible

web.dev/optimize-lcp/#… Image
Read 15 tweets
Mar 28
Performance tip: if you need to set a lot of dynamic keys to an object, a Map can give you better perf

Explanation in 🧵: Image
The answer has to do with monomorphism: mrale.ph/blog/2015/01/1…

In short, JS VMs try to assume a shape of an object using a hidden class. When the shape changes, this can lead to a deopt

It's the same reason why setting a property to null/undefined can be faster than deleting: Image
A Map, on the other hand, is optimized for this very use case of frequently adding and removing keys (see the "performance" table row here): developer.mozilla.org/en-US/docs/Web…
Read 7 tweets
Mar 22
Performance tip: did you know that even a simple chat widget can have a negative impact on your site performance?

But there is a solution: you can render a div that *looks* like the widget button, that on click downloads the actual widget JS only when needed

More info in 🧵:
We did this on the @builderio site to great success, and wouldn't have the performance score we have without it!
Note though: this method will not support all features of your chat widget platform.

If you want to use features like popping up messages to visitors automatically, you should use the standard embed code and pay the performance tax
Read 4 tweets
Mar 22
Performance is hard, and there are many things to know

That's why I have compiled many tips for image perf into this one cheat sheet!

More info in 🧵:
Great deep dive for much more info here: web.dev/image-componen…
Now, you may be thinking "that is a lot of work!"

Well, you are in luck. Image components provided @cloudinary, @nextjs, @GatsbyJS, and @nuxt_js do many of these optimizations (like nextgen format, dynamic srcset, etc) for you completely automatically!
Read 9 tweets
Mar 21
Performance tip: make your page load feel more instant by using priority hints to help the browser prioritize the most critical resources

More you can do with priority hints in 🧵:
You can also prioritize iframes, scripts, links, and fetch!

Learn more: web.dev/priority-hints/
If you are wondering "between scripts, images, and iframes, which would have the most impact on my site speed to put time into optimizing" - I got you: builder.io/c/performance-…
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(