# Web Performance Reference A consolidated reference guide covering web performance concepts, optimization techniques, and the Performance API, sourced from the Mozilla Developer Network (MDN). --- ## Table of Contents 1. [Web Performance Overview](#1-web-performance-overview) 2. [Performance Fundamentals](#2-performance-fundamentals) 3. [Performance Best Practices](#3-performance-best-practices) 4. [HTML Performance](#4-html-performance) 5. [JavaScript Performance](#5-javascript-performance) 6. [CSS Performance](#6-css-performance) 7. [Performance API](#7-performance-api) 8. [Performance Data](#8-performance-data) 9. [Server Timing](#9-server-timing) 10. [User Timing](#10-user-timing) --- ## 1. Web Performance Overview > **Source:** ### Definition **Web performance** encompasses: - Objective measurements (load time, frames per second, time to interactive) - Perceived user experience of load and response times - Smoothness during user interactions (scrolling, animations, button responsiveness) ### Recommended Timings | Target | Threshold | |--------|-----------| | Page load indication | 1 second | | Idling | 50ms | | Animations | 16.7ms (60 FPS) | | User input response | 50-200ms | Users abandon sites that respond slowly. The goal is to minimize loading and response times while adding features that conceal latency by maximizing availability and interactivity as soon as possible. ### Key Performance Metrics | Metric | Full Name | Definition | |--------|-----------|------------| | **FCP** | First Contentful Paint | First time any content appears | | **LCP** | Largest Contentful Paint | Largest content element visible | | **CLS** | Cumulative Layout Shift | Visual stability during interactions | | **INP** | Interaction to Next Paint | Responsiveness to user input | | **TTFB** | Time to First Byte | Server response time | | **TTI** | Time to Interactive | Page becomes fully interactive | | **Jank** | -- | Non-smooth animation or scrolling | ### Performance API Categories - **High-precision timing**: Sub-millisecond monitoring via stable monotonic clock - **Navigation Timing**: Metrics for page navigation (DOMContentLoaded, load time) - **Resource Timing**: Detailed network timing for individual resources - **User Timing**: Custom marks and measures - **Long Animation Frames (LoAF)**: Identifies janky animations - **Server Timing**: Backend performance metrics ### Related Browser APIs - **Page Visibility API**: Track document visibility state - **Background Tasks API** (`requestIdleCallback()`): Queue non-blocking tasks - **Intersection Observer API**: Asynchronously monitor element visibility - **Network Information API**: Detect connection type for adaptive content - **Battery Status API**: Optimize for power-constrained devices - **Beacon API**: Send performance data to analytics - **Media Capabilities API**: Check device media support ### Resource Loading Hints - **DNS-prefetch**: Pre-resolve domain names - **Preconnect**: Establish early connections - **Prefetch**: Load resources before needed - **Preload**: Load critical resources early ### Monitoring Approaches - **Real User Monitoring (RUM)**: Long-term trend analysis from actual users - **Synthetic Monitoring**: Controlled regression testing during development --- ## 2. Performance Fundamentals > **Source:** ### Why Web Performance Matters - Promotes accessibility and inclusive design - Enhances user experience and retention - Directly impacts business goals and conversions ### Core Components - Web page loading performance - Content rendering in browsers - User agent capabilities and constraints - Performance across different user groups ### Perceived Performance Metrics focused on user perception rather than raw milliseconds: - **Page load time** -- initial content availability - **Responsiveness** -- interaction feedback speed - **Animation smoothness** -- visual fluidity - **Scrolling smoothness** -- scroll interaction quality ### Optimization Areas | Area | Focus | Impact | |------|-------|--------| | **Multimedia (Images)** | Media optimization based on device capability, size, pixel density | Reduces bytes per image | | **Multimedia (Video)** | Video compression, audio track removal from background videos | Reduces file size | | **JavaScript** | Best practices for interactive experiences | Improves responsiveness, battery life | | **HTML** | DOM node minimization, optimal attribute ordering | Improves load and render time | | **CSS** | Feature-specific optimization | Prevents negative performance impact | ### Performance Strategy - **Performance budgets**: Set limits on asset sizes - **Performance culture**: Organizational commitment - **Regression prevention**: Avoid bloat over time - **Mobile-first approaches**: Responsive images and adaptive media delivery --- ## 3. Performance Best Practices > **Source:** ### Core Best Practices 1. **Learn the Critical Rendering Path** -- Understand how browsers render pages to optimize performance 2. **Use Resource Hints** -- `rel=preconnect`, `rel=dns-prefetch`, `rel=prefetch`, `rel=preload` 3. **Minimize JavaScript** -- Only load JavaScript needed for the current page 4. **Optimize CSS** -- Address CSS performance factors, load CSS asynchronously when possible 5. **Use HTTP/2** -- Deploy HTTP/2 on your server or CDN 6. **Use a CDN** -- Significantly reduces resource load times 7. **Compress Resources** -- Use gzip, Brotli, or Zopfli compression 8. **Optimize Images** -- Use CSS animations or SVG when possible 9. **Implement Lazy Loading** -- Load content outside viewport lazily; use the `loading` attribute on `` elements 10. **Focus on User Perception** -- Perceived performance matters as much as actual timing ### Asynchronous CSS Loading ```html ``` ```javascript const stylesheet = document.getElementById("my-stylesheet"); stylesheet.addEventListener("load", () => { stylesheet.media = "all"; }); ``` ### Critical CSS Inlining - Inline CSS for above-the-fold content using `