Resource Loading Optimization
Resource Compression and Merging
CSS/JS Compression Techniques:
- Compression Principles:
- Remove whitespace, comments, and line breaks.
- Shorten variable and function names (obfuscation).
- Eliminate dead code and unused code.
- Toolchain Options:
- Webpack: TerserPlugin (JavaScript), CssMinimizerPlugin (CSS).
- Rollup: Terser plugin.
- Vite: Built-in Terser compression.
- Compression Configuration Example:
// webpack.config.js
const TerserPlugin = require('terser-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
module.exports = {
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
compress: { drop_console: true },
mangle: true
}
}),
new CssMinimizerPlugin()
]
}
};Image Optimization Strategies:
- Format Selection Guide:
- JPEG: Suitable for photographic images.
- PNG: Ideal for images requiring transparency.
- WebP: Modern format with better compression rates.
- AVIF: Next-generation format with higher compression efficiency.
- Recommended Compression Tools:
- Squoosh: Google’s online image compression tool.
- ImageOptim: Image optimization tool for Mac.
- Sharp: Node.js image processing library.
- Responsive Image Implementation:
<picture>
<source srcset="image.avif" type="image/avif">
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt="Example image">
</picture>Caching Strategies
Cache Types Comparison:
| Cache Type | Description | Use Case | Example |
|---|---|---|---|
| Strong Cache | Uses cache directly without server requests | Infrequently changing static resources | Cache-Control: max-age=31536000 |
| Negotiated Cache | Validates resource changes with the server | Frequently updated resources | ETag/Last-Modified |
| Service Worker | Fully developer-controlled cache | Offline apps, PWAs | Custom caching logic |
Service Worker Cache Example:
// sw.js
const CACHE_NAME = 'v1';
const ASSETS = [
'/index.html',
'/styles/main.css',
'/scripts/main.js'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => cache.addAll(ASSETS))
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then(response => response || fetch(event.request))
);
});Cache Update Strategies:
- Versioned Filenames:
main.[hash].js - Cache Clearing: Periodically clear old caches.
- Cache Preheating: Preload critical resources.
Resource Preloading
Preloading Techniques Explained:
| Technique | Purpose | Priority | Use Case |
|---|---|---|---|
| preload | Preload critical resources for the current page | High | Critical CSS/JS |
| prefetch | Preload resources likely needed in the future | Low | Next-page resources |
| preconnect | Establish connections in advance | Medium | Third-party domains |
| dns-prefetch | Pre-resolve DNS | Low | Cross-domain resources |
Implementation Example:
<!-- Critical resource preloading -->
<link rel="preload" href="critical.css" as="style">
<link rel="preload" href="main.js" as="script">
<!-- Predictive prefetching -->
<link rel="prefetch" href="next-page.js" as="script">
<!-- Connection optimization -->
<link rel="preconnect" href="https://api.example.com">
<link rel="dns-prefetch" href="https://cdn.example.com">Dynamic Preloading Strategies:
// Preloading based on user behavior
document.addEventListener('mousemove', (e) => {
if (e.clientX > window.innerWidth - 100) {
// User may scroll, preload next page
import(/* webpackPrefetch: true */ './next-page');
}
});
// Route-based preloading
const routes = [
{
path: '/dashboard',
component: () => import(/* webpackPrefetch: true */ './Dashboard')
}
];Lazy Loading and On-Demand Loading
Image Lazy Loading Implementation:
- Intersection Observer API:
const lazyImages = document.querySelectorAll('img[data-src]');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
observer.unobserve(img);
}
});
});
lazyImages.forEach(img => observer.observe(img));- Traditional Scroll Event Implementation:
function lazyLoad() {
const images = document.querySelectorAll('img[data-src]');
images.forEach(img => {
if (img.getBoundingClientRect().top < window.innerHeight) {
img.src = img.dataset.src;
}
});
}
window.addEventListener('scroll', throttle(lazyLoad, 200));Route Lazy Loading:
// React.lazy + Suspense
const LazyComponent = React.lazy(() => import('./LazyComponent'));
function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
);
}
// Vue async component
const AsyncComponent = () => ({
component: import('./AsyncComponent.vue'),
loading: LoadingComponent,
error: ErrorComponent,
delay: 200,
timeout: 3000
});Resource Prioritization and Scheduling
Resource Hints Explained:
- preload: High-priority preload for current page resources.
- prefetch: Low-priority preload for future resources.
- preconnect: Pre-establish connections (DNS, TLS handshake, etc.).
- dns-prefetch: Pre-resolve DNS only.
Priority Control Example:
<!-- High-priority resources -->
<link rel="preload" href="main.css" as="style" importance="high">
<link rel="preload" href="main.js" as="script" importance="high">
<!-- Low-priority resources -->
<link rel="prefetch" href="next-page.js" as="script" importance="low">Resource Loading Order Optimization:
- Inline Critical CSS: Avoid render blocking.
- Asynchronous Non-Critical JS: Use
async/defer. - Font Preloading:
<link rel="preload" href="font.woff2" as="font">
Rendering Performance Optimization
Critical Rendering Path Optimization
CRP Optimization Steps:
- Reduce Render-Blocking Resources:
- Inline critical CSS.
- Asynchronously load non-critical JS.
- Optimize HTML Structure:
- Simplify DOM tree depth.
- Avoid deep nesting.
- CRP Analysis Tools:
- Chrome DevTools Coverage tool.
- WebPageTest First Meaningful Paint analysis.
Optimization Example:
<!-- Before optimization -->
<head>
<link rel="stylesheet" href="all.css">
<script src="analytics.js"></script>
</head>
<!-- After optimization -->
<head>
<style>/* Inline critical CSS */</style>
<link rel="preload" href="all.css" as="style" onload="this.rel='stylesheet'">
<script defer src="analytics.js"></script>
</head>CSS Optimization
Avoiding Repaints and Reflows:
- Repaint: Changes to element appearance without affecting layout.
- Reflow: Changes to element size or position affecting layout.
Optimization Strategies:
- Use
transformandopacity: Trigger GPU acceleration. - Avoid Frequent Style Changes: Batch style updates.
- Use
will-change: Hint to the browser about upcoming changes.
CSS Performance Pitfalls:
/* Bad: Triggers reflow */
.box {
width: 100px;
height: 100px;
transition: width 0.3s, height 0.3s;
}
/* Good: Triggers compositing only */
.box {
width: 100px;
height: 100px;
transform: scale(1);
transition: transform 0.3s;
}JavaScript Execution Optimization
Reducing Main Thread Blocking:
- Web Workers: Offload computationally intensive tasks to background threads.
- requestIdleCallback: Execute tasks during idle time.
- Task Chunking: Break large tasks into smaller ones.
Optimization Example:
// Use Web Worker for large data processing
const worker = new Worker('data-processor.js');
worker.postMessage(largeData);
worker.onmessage = (e) => {
// Process result
};
// Use requestIdleCallback
function processTask(deadline) {
while (deadline.timeRemaining() > 0 && tasks.length > 0) {
const task = tasks.pop();
task();
}
if (tasks.length > 0) {
requestIdleCallback(processTask);
}
}
requestIdleCallback(processTask);Event Handling Optimization:
- Event Delegation: Reduce the number of event listeners.
- Debouncing and Throttling: Control event trigger frequency.
- Passive Event Listeners: Improve scrolling performance.
Virtual DOM and Diff Algorithm Optimization
Virtual DOM Principles:
- Virtual DOM Tree: JavaScript object representation of the DOM structure.
- Diff Algorithm: Compares differences between old and new virtual DOM trees.
- Patch Process: Applies differences to the real DOM.
Optimization Strategies:
- Use Keys Wisely: Help the Diff algorithm identify nodes efficiently.
- Avoid Unnecessary Re-renders:
- Use
React.memo/PureComponent. - Implement
shouldComponentUpdate.
- Use
- Batch Updates: Reduce DOM operations.
Diff Algorithm Optimization Example:
// Bad: Creates new array on every render
function List({ items }) {
return (
<ul>
{items.map(item => (
<ListItem key={item.id} item={item} />
))}
</ul>
);
}
// Good: Avoids inline functions and objects
const MemoizedListItem = React.memo(ListItem);
function List({ items }) {
return (
<ul>
{items.map(item => (
<MemoizedListItem
key={item.id}
item={item}
onClick={handleClick} // Avoid inline functions
/>
))}
</ul>
);
}GPU Acceleration and Compositing Layer Optimization
GPU Acceleration Techniques:
- transform and opacity: Trigger GPU acceleration.
- will-change: Hint to the browser about upcoming changes.
- translateZ(0): Force creation of a separate layer (deprecated).
Compositing Layer Management:
- Use
will-changeJudiciously:
.animated-element {
will-change: transform, opacity;
}- Avoid Excessive Compositing Layers:
- Inspect with
chrome://flags/#composited-layer-borders. - Analyze using the Layers panel.
- Inspect with
- Animation Performance Optimization:
- Use
requestAnimationFrame. - Avoid reflows during animations.
- Use
Network Transmission Optimization
HTTP/2 and HTTP/3 Features
HTTP/2 Core Features:
- Multiplexing: Parallel multiple requests over a single connection.
- Header Compression: HPACK algorithm reduces overhead.
- Server Push: Proactively push resources to the client.
HTTP/3 New Features:
- QUIC Protocol: Resolves TCP head-of-line blocking.
- Improved Congestion Control: Faster connection establishment.
- Better Mobile Network Support: Reduced packet loss impact.
Protocol Upgrade Strategies:
- Server Configuration:
- Enable HTTP/2 (nginx:
http2 on;). - Prepare for HTTP/3 (requires QUIC support).
- Enable HTTP/2 (nginx:
- Client Compatibility:
- Modern browsers fully support HTTP/2.
- HTTP/3 requires specific client support.
CDN Acceleration and Edge Computing
CDN Principles:
- Content Delivery Network: Globally distributed server nodes.
- Intelligent Routing: Selects the nearest node to serve content.
- Caching Strategy: Caches static resources.
Edge Computing Applications:
- Edge Rendering: Generate HTML at edge nodes.
- Edge Caching: Cache dynamic content.
- Edge Functions: Execute simple logic at the edge.
CDN Configuration Example:
# nginx CDN configuration example
server {
listen 80;
server_name example.com;
location /static/ {
alias /var/www/static/;
expires 1y;
add_header Cache-Control "public";
}
location / {
proxy_pass http://backend;
}
}Data Compression and Encoding
Compression Algorithm Comparison:
| Algorithm | Compression Rate | Speed | CPU Usage | Use Case |
|---|---|---|---|---|
| Gzip | Moderate | Fast | Low | General |
| Brotli | High | Medium | Medium | Text resources |
| Zstandard | High | Fast | Medium | General |
Server Configuration Example:
# nginx Gzip configuration
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 6;
gzip_min_length 256;
# Brotli configuration (requires additional module)
brotli on;
brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;Client Detection:
// Detect supported compression algorithms
const acceptEncoding = navigator.userAgent.match(/Accept-Encoding:\s*(.*)/i)[1];
const supportsBrotli = acceptEncoding.includes('br');
const supportsGzip = acceptEncoding.includes('gzip');Reducing Network Requests
Resource Merging Strategies:
- CSS/JS Merging: Reduce HTTP requests.
- Sprite Sheets: Combine multiple small icons.
- Inline Critical Resources: Avoid render blocking.
Inline Resource Example:
<!-- Inline critical CSS -->
<style>
/* Critical CSS content */
</style>
<!-- Inline small images (Base64) -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." alt="Small icon">Resource Merging Considerations for HTTP/2:
- Trade-offs:
- HTTP/2 allows efficient parallel loading of multiple small files.
- Excessive merging may lead to cache invalidation.
- Recommended Strategies:
- Inline critical CSS.
- Load non-critical CSS/JS on demand.
- Keep large resources as separate files.
Network Latency and Packet Loss Optimization
Latency Optimization Techniques:
- Preconnect:
<link rel="preconnect"> - Preload:
<link rel="preload"> - DNS Prefetch:
<link rel="dns-prefetch">
Packet Loss Optimization Strategies:
- QUIC Protocol: Foundation of HTTP/3.
- Forward Error Correction (FEC): Send redundant data preemptively.
- Multipath Transmission: Use multiple network connections simultaneously.
Network Status Detection:
// Detect network connection type
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
if (connection) {
console.log('Network type:', connection.effectiveType);
console.log('RTT:', connection.rtt);
console.log('Downlink speed:', connection.downlink + 'Mb/s');
}
// Monitor network changes
connection.addEventListener('change', () => {
// Adjust resource loading strategy
});Offline Handling Solutions:
- Service Worker Caching: Enable offline access to core functionality.
- Local Storage: Save critical data.
- Retry Mechanism: Automatically retry when the network recovers.
Summary
Frontend performance optimization is a systematic process that requires comprehensive improvements across resource loading, rendering performance, and network transmission. Key aspects of modern web application performance optimization include:
- Reducing Critical Rendering Path Blocking: Through code splitting, lazy loading, and other techniques.
- Optimizing Resource Loading: Using caching, preloading, and compression effectively.
- Improving Rendering Efficiency: Avoiding repaints and reflows, optimizing CSS and JavaScript execution.
- Adapting to Network Conditions: Tailoring resource delivery strategies for varying network environments.
- Continuous Monitoring and Analysis: Leveraging performance tools to track optimization results.
By systematically applying these optimization techniques, web application performance can be significantly enhanced, improving user experience and achieving business objectives.



