Image Compression for Developers: Finding the Sweet Spot Between Quality and File Size
Images are often some of the heaviest assets served by websites. You can have well-optimised JavaScript, efficient CSS and a fast backend, but a few oversized images can still slow down your pages. As developers, we oft
Images are often some of the heaviest assets served by websites. You can have well-optimised JavaScript, efficient CSS and a fast backend, but a few oversized images can still slow down your pages.
As developers, we often focus on reducing JavaScript bundle sizes, lazy-loading components and optimising database queries. However, image optimisation deserves just as much attention.
The interesting thing is that you don't always need to preserve 100% image quality to deliver an excellent visual experience.
In this article, I'll explore how JPEG, WebP and AVIF compression work, how to choose practical quality settings, and what to consider when optimising images for production websites.
1. Why image compression matters
Consider a website with ten product images, each weighing approximately 1.2 MB.
That's around 12 MB of image data before accounting for other page assets.
Now imagine reducing those images to an average of 250 KB without noticeable visual degradation.
The total image payload drops to approximately 2.5 MB.
That's a substantial reduction in the amount of data visitors need to download.
Smaller images can help with:
- Faster page loading.
- Better Largest Contentful Paint (LCP).
- Reduced bandwidth consumption.
- Improved mobile browsing experiences.
- Lower data costs for users.
- Better performance on slower connections.
Image optimisation isn't simply about making files smaller. It's about finding the right balance between visual quality and delivery performance.
2. JPEG vs WebP vs AVIF: Which format should you use?
Choosing the right image format is one of the easiest ways to improve image delivery.
Here's a practical comparison:
| Format | Strengths | Best use cases |
|---|---|---|
| JPEG | Widely supported and compatible | Photographs and legacy systems |
| WebP | Good compression and broad browser support | General web images |
| AVIF | Often achieves smaller files at comparable visual quality | Performance-focused websites |
For most modern websites, WebP and AVIF are worth considering.
However, the smallest file isn't necessarily the best option. Encoding time, browser support, image characteristics and visual quality all matter.
You should also consider providing fallbacks where necessary.
3. What image quality setting should you use?
This is where things get interesting.
Many image compression tools expose a quality slider ranging from 0 to 100.
It's tempting to assume that 100 always means better performance or that reducing quality significantly will destroy an image.
Neither assumption is particularly useful.
For lossy compression, higher quality generally means larger files, but the relationship isn't linear.
A small reduction from 100 to 80 can sometimes produce a substantial reduction in file size without an obvious difference during normal viewing.
As a starting point, consider these settings:
| Format | Suggested starting range |
|---|---|
| JPEG | 75–85 |
| WebP | 75–85 |
| AVIF | 65–75 |
These are starting points, not universal rules. Different encoders interpret quality values differently, and photographs, illustrations, screenshots and text-heavy graphics behave differently during compression.
The best approach is to compare the output visually rather than relying exclusively on the numerical quality setting.
4. Don't optimise using file size alone
One mistake developers sometimes make is choosing the smallest possible image without checking the result.
An image might be 80% smaller but have visible artefacts around edges, text or skin tones.
This is where objective image quality metrics can help.
Some useful metrics include:
SSIM (Structural Similarity Index): Measures structural similarity between an original image and its compressed version.
PSNR (Peak Signal-to-Noise Ratio): Measures reconstruction quality based on pixel-level differences.
Visual inspection: Still important because numerical metrics don't always reflect what people actually notice.
Ideally, combine objective measurements with side-by-side visual comparisons.
5. My practical compression recommendations
For production websites, I'd start with the following approach:
Product photographs: Try WebP at quality 80 and compare against AVIF at quality 70.
Blog featured images: Use WebP or AVIF, resize to the actual display dimensions and test quality around 75–80.
Transparent graphics: Consider WebP or PNG depending on transparency requirements and the nature of the graphic.
Screenshots and text-heavy images: Be more conservative with lossy compression because small text and sharp edges can deteriorate quickly.
Thumbnails: Use appropriately smaller dimensions rather than simply compressing a full-resolution image.
Remember that resizing and compression solve different problems. If an image is displayed at 400 × 300 pixels, serving a 3000 × 2000 pixel original is often unnecessary.
6. Automate image optimisation in your development workflow
Manually compressing every image isn't practical for larger projects.
Consider integrating image optimisation into your workflow.
For example:
- Use Next.js Image for responsive image delivery.
- Generate WebP or AVIF variants during image processing.
- Use lazy loading for below-the-fold images.
- Specify image width and height to reduce layout shifts.
- Serve appropriately sized responsive images using srcset.
- Monitor image payloads during performance testing.
For projects with user-uploaded images, consider a pipeline that resizes, validates and optimises images before they become publicly accessible.
7. Test the results yourself
I recently explored this subject in more detail by comparing compression quality settings across JPEG, WebP and AVIF.
The experiment examines how file size changes as quality settings decrease, alongside structural similarity measurements and visual inspection.
One interesting finding from the test was how much file size could be reduced around the Q80 range without an obvious visual difference on the test photograph.
You can read the full comparison, including the test results and quality breakdown, here:
Best Image Compression Quality Settings – Vexelloom
You can also experiment with different compression settings using browser-based image processing tools to see how your own images respond.
Final thoughts
Image optimisation is one of those areas where small engineering decisions can have a noticeable impact on the overall user experience.
Rather than automatically choosing maximum quality, test different formats, experiment with compression settings and compare the results.
A good optimisation strategy should deliver images that look great without unnecessarily increasing page weight.
I'd be interested to hear how other developers approach image optimisation.
**What format do you primarily use in production: JPEG, WebP or AVIF? And what quality settings have worked best for your projects?
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.