Day 19: Performance Optimization in Frontend System Design - Part 2
Mohammed Saif
Software Engineer | Next | Tailwind | Redux | Material UI | React | Javascript | Community Volunteer
Welcome back to my journey in learning frontend system design! Today, we delve into network optimization techniques to enhance performance. But before we get technical, let's start with a story.
The Concert and the Roadies: A Story on Network Optimization
In a small town, there was an annual music festival that everyone eagerly awaited. The festival had multiple stages and bands performing, but there was always a problem: setting up the equipment took a long time, causing delays in performances. One year, a new group of roadies came up with a brilliant idea to optimize the setup process.
Instead of waiting for one band to finish before starting to set up the next, they began preparing the equipment for upcoming performances in advance. They identified the critical instruments needed first and ensured they were ready to go as soon as the previous act finished. They also prioritized the most complex setups, starting them even earlier. With this strategy, the festival ran smoothly, and the crowd enjoyed continuous music without long waits.
Similarly, network optimization in web development ensures that your website's resources — scripts, styles, and other assets — are delivered to users quickly and efficiently.
Network Optimization Techniques
1. Async JavaScript: Async/Defer
When a web page loads, it often encounters JavaScript files. These can be roadblocks, halting the HTML parsing until the JavaScript is fully downloaded and executed. Let's explore the different ways to handle this:
a) <script>:
.
b) <script async>:
.
c) <script defer>:
.
2. Resource Hinting
When making requests to a server, several steps are involved: DNS lookup, HTTP connection, SSL handshake, etc. These steps can add latency. Resource hinting techniques can mitigate this:
<link rel="preconnect" crossorigin/>
领英推荐
<link rel="dns-prefetch" crossorigin/>
<link rel="preload" as="image"/>
<link rel="prefetch" as="style"/>
<link rel="prerender" />
Advanced Techniques and Tips:
3. Resource Hint Priority
To avoid overwhelming the browser with too many hints, prioritize resources:
a) Lower priority for non-critical preloaded scripts:
<link rel="preload" as="script" fetchpriority="low"/>
b) Preloaded CSS without blocking other resources:
<link rel="preload" as="style" fetchpriority="low" onload="this.rel='stylesheet'"/>
4. Early Hints
The 103 status code allows servers to send preliminary headers to the browser, hinting at critical resources to fetch even before the main response is ready. This can drastically reduce the initial page load time.
5. HTTP Upgrade (HTTP/1.1 vs HTTP/2 vs HTTP/3)
By implementing these network optimization techniques, we can ensure faster, smoother, and more efficient web experiences. Stay tuned for more insights as I continue my journey in frontend system design