跪拜 Guibai
← Back to the summary

Image Performance Optimization: Responsive Delivery, Modern Formats, and the DPR Sweet Spot

This article covers another major area of performance optimization — “Image Performance Optimization.”

Images are usually the largest and most bandwidth-hungry resources on a webpage. The core idea is: “Use various techniques to make the browser download the fewest image bytes possible without sacrificing visible image quality.”

The main content can be summarized into the following four core technical dimensions:

1. Responsive Images and Size Optimization (Dimensions & Density)

2. Modern File Formats and Compression (Formats & Compression)

3. Server-Side Dynamic Delivery (Content Negotiation)

4. Browser Rendering and Loading Control (Loading & Decoding)

Summary: The previous chapter discussed using "resource hints" to control download timing, while this chapter discusses using "adaptive techniques" to control the size of the resource itself. Large companies typically combine loading="lazy" (for off-screen delay) + modern formats (AVIF/WebP) + responsive dimensions (srcset) for image optimization.

Questions

What is DPR? Why do different screens require different versions of the same image, and how do you calculate the right one?

This is another extremely core topic in frontend performance optimization and mobile adaptation. Senior interviewers at large companies love using it to test your low-level understanding of "mobile physical pixels and logical pixels."

Let's break down DPR to see how it troubles frontend developers and how to calculate and optimize for it.

1. What exactly is DPR (Device Pixel Ratio)?

DPR stands for Device Pixel Ratio. Its formula is very simple:

DPR = Physical Pixels (Device Pixels) / Logical Pixels (CSS Pixels)

💡 Why does this exist?

Before 2010 (the iPhone 3 era), 1px of CSS logical pixel = 1 physical light-emitting dot, meaning DPR=1. But in 2010, the iPhone 4 introduced the Retina screen. Its physical size was the same as the iPhone 3, but Apple crammed in twice the number of physical light-emitting dots.

If 1px CSS pixel still corresponded to 1 physical light-emitting dot, the entire webpage would appear shrunk to half its size on the iPhone 4.

So Apple invented a grid mapping: on the iPhone 4, 1px of CSS logical pixel corresponds to 2 physical pixels horizontally and 2 physical pixels vertically. In other words, 2×2=4 physical pixels are used to render 1 CSS pixel. This way, the webpage size remains the same, but text and lines become extremely sharp and free of jagged edges. This is called DPR=2.

2. Why do different screens require different versions of the same image?

This is the root cause of "why images look blurry" or "why data is wasted."

An image is composed of a grid of "pixels (intrinsic dimensions)." Suppose you write a container in your page with width: 500px; height: 500px;.

⚠️ The contradiction arises:

Can't we just send everyone the 1500x1500px ultra-HD image? No! If a user with an old DPR = 1 computer downloads a 1500x1500px image, their screen can only display 500x500. The browser will downsample (compress) it, and the user won't see any visible difference, but they will have wasted 9 times the file size in downloads (the area ratio is 3×3=9), wasting the large company's expensive CDN bandwidth and the user's mobile data.

3. How to calculate and find this "sweet spot"?

The core optimization strategy of large companies is: Adaptive delivery — calculate the device's DPR and container width, and give it exactly what it needs.

There are two mainstream calculation and implementation approaches in the industry:

Approach 1: Modern HTML5 Tag Auto-Calculation (Srcset + Sizes)

Let the browser do the calculation. You just prepare images of various sizes and provide hints in the code:

HTML

<img 
  src="img-500.jpg" 
  sizes="(min-width: 768px) 500px, 100vw"
  srcset="img-500.jpg 500w, img-1000.jpg 1000w, img-1500.jpg 1500w"
  alt="example"
>

Approach 2: Image CDN Dynamic Cropping (Most Common in Large Companies)

Large companies (like Alibaba, Tencent, ByteDance) all have dedicated image CDN services. The frontend can calculate the current device's DPR via JS, then concatenate URL parameters to request the image from the backend.

JavaScript

// 1. Get the current device's DPR
const dpr = window.devicePixelRatio || 1;

// 2. Assume the image container's CSS width is 300px in the current layout
const containerWidth = 300;

// 3. Calculate the perfect physical image width
const targetWidth = containerWidth * dpr; // If dpr is 2, this is 600

// 4. Dynamically concatenate Alibaba Cloud/Tencent Cloud CDN cropping parameters
const imageUrl = `https://cdn.example.com/product.jpg?x-oss-process=image/resize,w_${targetWidth}`;

// 5. Assign this URL to the img tag
myImage.src = imageUrl;

💡 Follow-up: The human eye has limits (a money-saving trick for large companies)

The interviewer might dig deeper: "Since DPR = 3 requires a 3x image, do we need to pursue infinite sharpness?"