Why Performance Matters
Website performance directly impacts user experience, conversion rates, and SEO rankings. Studies show that:
- 53% of mobile users abandon sites that take longer than 3 seconds to load
- Every 100ms delay in load time can decrease conversions by 1%
- Google uses page speed as a ranking factor
Let's explore practical techniques to make your website faster.
1. Optimize Images
Images typically account for 50-70% of a website's total size.
Use modern formats
<!-- Use WebP with fallback -->
<picture>
<source srcset="image.webp" type="image/webp" />
<img src="image.jpg" alt="Description" />
</picture>
Lazy load images
// Native lazy loading
<img src="image.jpg" loading="lazy" alt="Description" />
Key practices:
- Compress images (TinyPNG, ImageOptim)
- Use appropriate dimensions (don't load 4000px images for 400px displays)
- Implement responsive images with
srcset
2. Minimize and Bundle JavaScript
Reduce JavaScript payload:
// Before: Multiple imports
import _ from 'lodash'
const result = _.debounce(fn, 300)
// After: Import only what you need
import debounce from 'lodash/debounce'
const result = debounce(fn, 300)
Code splitting
Split your code into smaller chunks:
// Dynamic imports
const HeavyComponent = () => import('./HeavyComponent.vue')
// Load only when needed
if (userWantsFeature) {
const module = await import('./feature.js')
module.init()
}
3. Implement Caching Strategies
Browser caching
# Nginx configuration
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
Service Workers
Cache assets for offline access:
// service-worker.js
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open('v1').then((cache) => {
return cache.addAll([
'/',
'/styles/main.css',
'/scripts/main.js'
])
})
)
})
4. Optimize CSS Delivery
Critical CSS
Inline critical CSS for above-the-fold content:
<head>
<style>
/* Critical CSS inline */
.hero { background: #000; height: 100vh; }
</style>
<link rel="preload" href="/styles/main.css" as="style" onload="this.rel='stylesheet'" />
</head>
Remove unused CSS
Use tools like PurgeCSS to eliminate dead code:
// Tailwind CSS config
module.exports = {
purge: ['./src/**/*.{js,jsx,ts,tsx,vue}'],
// ...
}
5. Use Content Delivery Networks (CDN)
CDNs serve content from servers closest to users:
- Cloudflare: Free tier available
- AWS CloudFront: Integrates with AWS services
- Vercel/Netlify: Automatic CDN for static sites
Benefits:
- Reduced latency
- Better global performance
- DDoS protection
- Automatic optimization
6. Optimize Fonts
Web fonts can significantly impact load time:
/* Preload critical fonts */
<link rel="preload" href="/fonts/main.woff2" as="font" type="font/woff2" crossorigin>
/* Use font-display for better UX */
@font-face {
font-family: 'CustomFont';
src: url('/fonts/custom.woff2') format('woff2');
font-display: swap; /* Show fallback immediately */
}
Best practices:
- Use system fonts when possible
- Limit font weights and variants
- Use WOFF2 format
- Self-host fonts to avoid external requests
7. Database Query Optimization
Slow database queries kill performance:
-- Before: Full table scan
SELECT * FROM users WHERE email LIKE '%@example.com';
-- After: Indexed query
CREATE INDEX idx_users_email ON users(email);
SELECT id, name, email FROM users WHERE email = '[email protected]';
Tips:
- Add indexes on frequently queried columns
- Use
SELECTspecific columns, not* - Implement database connection pooling
- Cache frequent queries (Redis, Memcached)
8. Enable Compression
Compress text-based assets:
# Enable Gzip compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
gzip_min_length 1000;
Modern alternative: Brotli compression offers 15-25% better compression than Gzip.
9. Monitor and Measure
You can't improve what you don't measure:
Tools:
- Google PageSpeed Insights: Overall performance score
- Lighthouse: Detailed audits
- WebPageTest: Real-world testing
- Chrome DevTools: Network and performance profiling
Key metrics:
- LCP (Largest Contentful Paint): < 2.5s
- FID (First Input Delay): < 100ms
- CLS (Cumulative Layout Shift): < 0.1
10. Server-Side Optimization
Use HTTP/2 or HTTP/3
listen 443 ssl http2;
Benefits: multiplexing, header compression, server push
Implement server-side caching
// Node.js example with Redis
const redis = require('redis')
const client = redis.createClient()
app.get('/api/data', async (req, res) => {
const cached = await client.get('data')
if (cached) {
return res.json(JSON.parse(cached))
}
const data = await fetchFromDatabase()
await client.setex('data', 3600, JSON.stringify(data))
res.json(data)
})
Real-World Impact
After implementing these techniques for a client's e-commerce site:
- Load time: Reduced from 4.2s to 1.3s
- Bounce rate: Decreased by 35%
- Conversion rate: Increased by 22%
- Google ranking: Improved from page 3 to page 1
Conclusion
Web performance optimization is an ongoing process. Start with the biggest wins (images, caching, code splitting), measure the impact, and iterate. Every millisecond counts when it comes to user experience and business metrics.
