The Fight For The Main Thread<\/h1>\nGeoff Graham<\/address>\n 2023-10-24T18:00:00+00:00
\n 2024-08-30T10:05:08+00:00
\n <\/header>\n
This article is sponsored by SpeedCurve<\/b><\/p>\n
Performance work is one of those things, as they say, that ought to happen in development. You know, have a plan for it and write code that\u2019s mindful about adding extra weight to the page.<\/p>\n
But not everything about performance happens directly at the code level, right? I\u2019d say many — if not most — sites and apps rely on some number of third-party scripts where we might not have any influence over the code. Analytics is a good example. Writing a hand-spun analytics tracking dashboard isn\u2019t what my clients really want to pay me for, so I\u2019ll drop in the ol\u2019 Google Analytics script and maybe never think of it again.<\/p>\n
That\u2019s one example and a common one at that. But what\u2019s also common is managing multiple third-party scripts on a single page. One of my clients is big into user tracking, so in addition to a script for analytics, they\u2019re also running third-party scripts for heatmaps, cart abandonments, and personalized recommendations — typical e-commerce stuff. All of that is dumped on any given page in one fell swoop courtesy of Google Tag Manager (GTM), which allows us to deploy and run scripts without having to go through the pain of re-deploying the entire site.<\/p>\n
As a result, adding and executing scripts is a fairly trivial task. It is so effortless, in fact, that even non-developers on the team have contributed their own fair share of scripts, many of which I have no clue what they do. The boss wants something, and it\u2019s going to happen one way or another, and GTM facilitates that work without friction between teams.<\/p>\n
All of this adds up to what I often hear described as a \u201cfight for the main thread.\u201d That\u2019s when I started hearing more performance-related jargon, like web workers, Core Web Vitals, deferring scripts, and using pre-connect, among others. But what I\u2019ve started learning is that these technical terms for performance make up an arsenal of tools to combat performance bottlenecks.<\/strong><\/p>\nThe real fight, it seems, is evaluating our needs as developers and stakeholders against a user\u2019s needs, namely, the need for a fast and frictionless page load.<\/p>\n
Fighting For The Main Thread<\/h2>\n
We\u2019re talking about performance in the context of JavaScript, but there are lots of things that happen during a page load. The HTML is parsed. Same deal with CSS. Elements are rendered. JavaScript is loaded, and scripts are executed.<\/p>\n
All of this happens on the main thread<\/strong>. I\u2019ve heard the main thread described as a highway that gets cars from Point A to Point B; the more cars that are added to the road, the more crowded it gets and the more time it takes for cars to complete their trip. That\u2019s accurate, I think, but we can take it a little further because this particular highway has just one lane<\/em>, and it only goes in one direction<\/em>. My mind thinks of San Francisco\u2019s Lombard Street, a twisty one-way path of a tourist trap on a steep decline.<\/p>\n
\n 2024-08-30T10:05:08+00:00
\n <\/header>\n
The real fight, it seems, is evaluating our needs as developers and stakeholders against a user\u2019s needs, namely, the need for a fast and frictionless page load.<\/p>\n
Fighting For The Main Thread<\/h2>\n
We\u2019re talking about performance in the context of JavaScript, but there are lots of things that happen during a page load. The HTML is parsed. Same deal with CSS. Elements are rendered. JavaScript is loaded, and scripts are executed.<\/p>\n
All of this happens on the main thread<\/strong>. I\u2019ve heard the main thread described as a highway that gets cars from Point A to Point B; the more cars that are added to the road, the more crowded it gets and the more time it takes for cars to complete their trip. That\u2019s accurate, I think, but we can take it a little further because this particular highway has just one lane<\/em>, and it only goes in one direction<\/em>. My mind thinks of San Francisco\u2019s Lombard Street, a twisty one-way path of a tourist trap on a steep decline.<\/p>\n