For a UI screenshot, palette: true beats every compressionLevel. Our 1440 × 900 screenshot was 430.1 KB at sharp's default level 6, 392.9 KB at level 9, and 244.3 KB as a palette PNG at 58.79 dB PSNR. The surprise is colours: asking for 256, 64 or 32 gave the same file. Only 16, 4 and 2 changed the output.
This article shows the script, every number, and the PNG options that did less than the docs suggest.
What does compressionLevel do in sharp's png()?
compressionLevel is the zlib level, 0 (fastest, largest) to 9 (slowest, smallest), with a default of 6, per the sharp output docs. PNG is lossless, so the level changes only size and time, never a pixel.
palette: true is different. It quantises the image to an indexed palette, which is lossy. The same docs list quality, colours, effort and dither as palette options, and say each one switches palette on.
How much does each compressionLevel save?
I decoded our pricing page screenshot (2880 × 1800) once, resized it to 1440 × 900, and timed only the PNG encode.
import sharp from 'sharp';
const { data, info } = await sharp('screenshot.png').resize({ width: 1440 })
.removeAlpha().raw().toBuffer({ resolveWithObject: true });
const raw = { raw: { width: info.width, height: info.height, channels: 3 } };
const median = (a) => [...a].sort((x, y) => x - y)[Math.floor(a.length / 2)];
for (const compressionLevel of [0, 1, 3, 6, 9]) {
const times = [];
let bytes = 0;
for (let i = 0; i < 5; i++) {
const t0 = performance.now();
const out = await sharp(data, raw).png({ compressionLevel }).toBuffer();
times.push(performance.now() - t0);
bytes = out.length;
}
console.log(`level ${compressionLevel} ${(bytes / 1000).toFixed(1)} KB ${Math.round(median(times))} ms`);
}
level 0 3895.2 KB 10 ms
level 1 707.9 KB 42 ms
level 3 470.3 KB 18 ms
level 6 430.1 KB 39 ms
level 9 392.9 KB 303 ms
Level 0 is not compression at all: 9.1 times the size of level 6. Level 1 is already within reach: 64.6% larger than level 6, at 42 ms. From 6 to 9 you save 37.1 KB (8.6%) and pay 7.8 times the time, 39 ms to 303 ms.
The timings are not monotonic. Level 3 (18 ms) ran faster than level 1 (42 ms). I ran each setting five times and kept the median, and I did not dig into why.
Note:
adaptiveFiltering: truemade the file bigger. At level 9 it wrote 464.3 KB in 627 ms, against 392.9 KB in 277 ms with it off. That is 18.2% larger and slower on a screenshot. Leave it off unless you have measured a gain.
What does palette: true change?
It turns a 24-bit image into an 8-bit indexed one, and the file shrinks a lot.
const out = await sharp(data, raw).png({ palette: true }).toBuffer();
palette (defaults) 244.3 KB 157 ms PSNR 58.79 dB
That is 43.2% smaller than level 6 and 37.8% smaller than level 9. A PSNR of 58.79 dB is very high. For flat UI, palette is the first thing to try. Our guide on making a PNG file smaller measures the same move on other images.
A photo is different. For the 1920 × 1076 camera photo (median of 3 runs), level 6 wrote 4,405.3 KB, level 9 wrote 4,177.9 KB (5.2% less), and palette wrote 665.9 KB at only 36.07 dB and took 1,377 ms. The palette is 6.6 times smaller, but 36.07 dB is a clear drop and I did not inspect the sky for banding. For photos, use a different format.
Why did colours 256, 64 and 32 give the same file?
Here is colours on the screenshot:
colours 256 244.3 KB PSNR 58.79 dB
colours 64 244.3 KB PSNR 58.79 dB
colours 32 244.3 KB PSNR 58.79 dB
colours 16 120.5 KB PSNR 41.09 dB
colours 8 120.5 KB PSNR 41.09 dB
colours 4 37.9 KB PSNR 29.75 dB
colours 2 10.7 KB PSNR 26.56 dB
The sizes step at 256, 16, 4 and 2, not at every value. My explanation, which the docs do not state: PNG palettes store 1, 2, 4 or 8 bits per pixel, so any limit between 17 and 256 produces an 8-bit file with the same pixels. I did not confirm this in the sharp source. What I know is the output: 64 and 32 colours did not save a byte.
The practical rule: only use colours at 16, 4 or 2 on purpose, and treat the values in between as 256.
What do quality and dither do?
quality is the better dial, but it is not smooth:
quality 100 244.3 KB PSNR 58.79 dB
quality 90 132.7 KB PSNR 44.61 dB
quality 80 148.6 KB PSNR 41.25 dB
quality 60 108.4 KB PSNR 39.02 dB
quality 40 106.2 KB PSNR 37.75 dB
Quality 90 wrote a smaller file than quality 80 (132.7 KB against 148.6 KB) and scored higher. I did not dig into why. Test two or three values on your own images; do not assume it is monotone.
dither surprised me more. At 16 colours on the same screenshot:
dither 0 76.6 KB PSNR 41.35 dB
dither 0.5 85.0 KB PSNR 41.30 dB
dither 1.0 120.5 KB PSNR 41.09 dB
Turning dithering off made the file 36.4% smaller than sharp's default of 1.0 and slightly higher scoring. Dither adds noise to fake gradients, and a UI screenshot has few. Dither may matter more on a photo with smooth gradients; I did not test that.
Does palette effort matter?
A little. effort for palette PNG runs from 1 to 10, with a default of 7:
effort 1 216.5 KB PSNR 51.66 dB 91 ms
effort 4 246.4 KB PSNR 57.61 dB 92 ms
effort 7 244.3 KB PSNR 58.79 dB 138 ms
effort 10 242.1 KB PSNR 58.74 dB 141 ms
Effort 1 is smaller but 7.1 dB worse. Effort 7 and 10 differ by 2.2 KB. Keep the default.
Which PNG settings should you use?
-
UI screenshots, diagrams, logos:
png({ palette: true, dither: 0 }), then check the result. Tryquality: 90if you want it smaller. -
Anything that must stay lossless:
compressionLevel: 9, adaptive filtering off, once per file. - Photos: do not use PNG. A JPEG or WebP will be far smaller.
How I measured
- Screenshot: our pricing page,
apps/web/assets/reference/screenshot.png, 2880 × 1800, resized to 1440 × 900 (RGB, no alpha). Photo:apps/web/public/images/index/image-compressor-sample-camera-photo.jpeg, resized to 1920 × 1076. - Versions: Node 24.22.0, sharp 0.35.4, libvips 8.18.6, libpng 1.6.58, libimagequant 2.4.1, zlib-ng 2.3.3.
- Time: encode only, median of 5 runs (3 for the photo). PSNR: RGB against the unquantised pixels, one encode per setting.
- Machine: Apple M1 Pro, macOS, 2026-10-18. Harness:
apps/web/scripts/devto-images/_measure-sharp-tuning.mjsand_measure-sharp-tuning-extra.mjs; the blocks above aremeasured.json, reformatted.
What I did not test
PNG with transparency, 16-bit PNG, interlacing, and the dither option on a photo. One screenshot is not every UI.
I build Blinkimg, an image compressor and converter with a developer API. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.

Top comments (0)