Sharp vs Squoosh: which to pick?
Originally published: 7 January 2024
No clear winner here. Horses for courses.
Here is a 1000 word blog post comparing Sharp and Squoosh for image generation with Astro:
Sharp vs Squoosh: Image Generation for Astro
Generating optimized images is essential for fast websites. Two great JavaScript image processing libraries to use with Astro are Sharp and Squoosh. In this post, we’ll compare their capabilities and performance.
Introduction
Sharp and Squoosh allow Node.js and Astro to resize, compress, and convert images on the fly. This saves storage space and speeds up sites.
Sharp has been around since 2016, while Squoosh is newer, launching in 2018. But they share similarities:
- Written in AssemblyScript and WebAssembly for native performance
- Support image formats like JPG, PNG, WebP, AVIF
- Resize, compress, and process images
- Expose a JavaScript API
Let’s dive into each library’s capabilities, performance, and use cases.
Capabilities
Sharp
Sharp has a robust feature set:
- Resize images with high quality Lanczos 3 resampling
- Output to various formats like PNG, JPG, WebP, AVIF
- Fine-grained compression control
- Cropping and trimming
- Rotation and flipping
- Blurring, sharpening and other filters
- Metadata extraction
- Progressive JPEG scan loading
- Read image data as a Buffer
- Parallelization with workers
Sharp covers most common image processing needs. The API focuses on programmatic control versus preset configurations.
Squoosh
Squoosh takes a slightly different approach:
- Presets for common use cases like “web” and “photo”
- Outputs to JPG, PNG, WebP, AVIF
- Auto mode automatically picks best format
- Resize images
- Background compression through WebAssembly
- Rotate, flip, watermark and filters
- Adaptive quality levels
- Progressive JPEGs
- Can compare compression of images
- Integrates with Emscripten ecosystem
Squoosh provides presets for ease of use, alongside lower-level control. The “auto” mode is handy for simple cases.
Both libraries cover the core essentials. Squoosh aims for simplicity, while Sharp has more advanced and programmatic features.
Performance
Image processing performance matters, especially at scale:
Sharp
- Leverages libvips for speed
- CPU multi-threading and GPU utilization
- Parallel runner built-in
- Can use ImageMagick and GraphicsMagick backends
Squoosh
- WebAssembly for near-native performance
- Multi-threading with Emscripten
- SIMD and Web Worker support
- WASM compiled output focused on speed
Sharp may have a slight edge performance-wise. However both are fast enough for most use cases.
For ultra high throughput processing, Sharp’s parallel runner helps scale across cores and machines.
Using With Astro
We can utilize both Sharp and Squoosh in our Astro components:
// Sharp example
import sharp from 'sharp';
const optimizedImage = sharp(src)
.resize(width, height)
.webp()
.toBuffer();
// Squoosh
import { squoosh } from 'astro-imagetools';
const optimizedImage = squoosh(src, {
widths: [480, 800],
formats: ['webp', 'avif'],
});
Their APIs work similarly for astro-imagetools. We can generate multiple sizes and formats.
Sharp gives more low-level control, while Squoosh simplifies with presets. Both will handle typical image optimization needs.
Conclusion
Sharp and Squoosh are two excellent choices for image processing with Astro. Sharp provides a rich set of customization options and optimizations for speed. Squoosh simplifies common use cases with handy presets and auto-mode.
For most sites, either library will cover the basics like resizing, compression and format conversion. Sharp excels at advanced programmatic manipulation, while Squoosh makes it easy to get started. Combine them with Astro for lightweight, optimized image generation!
Read Count: 0