SVG performance optimization is crucial for improving web page load speed and user experience. Here are detailed strategies for SVG performance optimization:
1. File Size Optimization
Remove Unnecessary Code:
bash# Optimize SVG with SVGO npx svgo input.svg -o output.svg # Batch optimize npx svgo -f ./icons -o ./optimized # Configure optimization options npx svgo --config svgo.config.js input.svg -o output.svg
SVGO Configuration Example:
javascript// svgo.config.js module.exports = { plugins: [ 'removeDoctype', 'removeXMLProcInst', 'removeComments', 'removeMetadata', 'removeUselessDefs', 'cleanupIDs', 'minifyStyles', 'convertPathData', 'mergePaths', 'removeUnusedNS', 'sortDefsChildren', 'removeEmptyAttrs', 'removeEmptyContainers', 'cleanupNumericValues', 'convertColors', 'removeUnknownsAndDefaults' ] };
2. Path Optimization
Simplify Path Commands:
svg<!-- Before optimization --> <path d="M 10.123456 20.654321 L 30.987654 40.321098" /> <!-- After optimization --> <path d="M10.12 20.65L30.99 40.32" />
Use Relative Coordinates:
svg<!-- Use absolute coordinates --> <path d="M 10 10 L 20 10 L 20 20 L 10 20 Z" /> <!-- Use relative coordinates (more concise)--> <path d="M10 10h10v10h-10z" />
Merge Paths:
svg<!-- Before optimization: multiple independent elements --> <rect x="10" y="10" width="50" height="50" fill="blue" /> <rect x="70" y="10" width="50" height="50" fill="blue" /> <!-- After optimization: use path --> <path d="M10 10h50v50H10z M70 10h50v50H70z" fill="blue" />
3. Rendering Performance Optimization
Reduce Element Count:
svg<!-- Before optimization: multiple independent elements --> <circle cx="10" cy="10" r="5" fill="blue" /> <circle cx="20" cy="10" r="5" fill="blue" /> <circle cx="30" cy="10" r="5" fill="blue" /> <!-- After optimization: use group --> <g fill="blue"> <circle cx="10" cy="10" r="5" /> <circle cx="20" cy="10" r="5" /> <circle cx="30" cy="10" r="5" /> </g>
Use CSS Instead of SVG Attributes:
svg<!-- Before optimization --> <circle cx="50" cy="50" r="40" fill="blue" stroke="red" stroke-width="2" /> <!-- After optimization: use CSS --> <style> .circle { fill: blue; stroke: red; stroke-width: 2px; } </style> <circle class="circle" cx="50" cy="50" r="40" />
4. Animation Performance Optimization
Prioritize CSS Animations:
css/* CSS animation (GPU accelerated) */ .animated { animation: rotate 2s linear infinite; transform-origin: center; } @keyframes rotate { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
Avoid Animating width/height:
css/* Before optimization: animate width/height (poor performance) */ .bad { animation: scale 1s ease; } @keyframes scale { from { width: 50px; height: 50px; } to { width: 100px; height: 100px; } } /* After optimization: animate transform (good performance) */ .good { animation: scale 1s ease; } @keyframes scale { from { transform: scale(1); } to { transform: scale(2); } }
5. Filter Performance Optimization
Avoid Overusing Filters:
svg<!-- Avoid complex filter chains --> <filter id="complex"> <feGaussianBlur stdDeviation="5" /> <feOffset dx="5" dy="5" /> <feColorMatrix type="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0" /> <feMerge> <feMergeNode in="SourceGraphic" /> <feMergeNode /> </feMerge> </filter> <!-- Simplify filters or use CSS --> <style> .shadow { filter: drop-shadow(3px 3px 3px rgba(0,0,0,0.3)); } </style>
6. Loading Optimization
Inline Critical SVGs:
html<!-- Inline first-screen critical SVGs --> <header> <svg viewBox="0 0 24 24" width="24" height="24"> <path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" /> </svg> </header> <!-- Lazy load non-critical SVGs --> <img src="non-critical.svg" loading="lazy" alt="Non-critical" />
Use SVG Sprite:
html<!-- Merge multiple icons into one file --> <svg style="display: none;"> <symbol id="icon1" viewBox="0 0 24 24">...</symbol> <symbol id="icon2" viewBox="0 0 24 24">...</symbol> <symbol id="icon3" viewBox="0 0 24 24">...</symbol> </svg> <!-- Use icons --> <svg><use href="#icon1" /></svg> <svg><use href="#icon2" /></svg>
7. Compression and Caching
Server Configuration:
nginx# Nginx configuration location ~* \.(svg)$ { gzip on; gzip_vary on; gzip_min_length 1000; gzip_types image/svg+xml; expires 1y; add_header Cache-Control "public, immutable"; }
8. Monitoring and Testing
Performance Testing Tools:
- Lighthouse: Test overall performance
- WebPageTest: Analyze load performance
- Chrome DevTools: Monitor rendering performance
Best Practices:
- Regularly optimize SVG files with SVGO
- Prioritize CSS animations
- Reduce element count and complexity
- Reasonably use filters and gradients
- Inline critical SVGs, lazy load others
- Enable gzip compression
- Set appropriate caching strategies
- Monitor performance metrics