Bun.Image: Native Image Processing in the Runtime
Better Stackgo watch the original →
the gist
Bun 1.3.14 introduces a built-in image processing API that eliminates native dependencies like libvips, offering faster performance than Sharp and native support for resizing, format conversion, and placeholder generation.
The Breakthrough
Bun 1.3.14 introduced Bun.Image, a built-in API that performs image resizing, cropping, rotation, and format conversion without requiring external native dependencies like libvips.
What Actually Worked
- Simplified Pipeline: Developers can process images directly within the runtime using a concise API, replacing complex
Sharpconfigurations that often fail in CI/CD pipelines due to native binary mismatches. - Efficient Resizing: The API handles resizing and format conversion (JPEG, PNG, WebP, HEIC, AVIF) with minimal code, as shown in this example:
const image = await Bun.file("input.jpg").image(); const optimized = await image .resize({ width: 800 }) .toFormat("webp", { quality: 80 }); await Bun.write("output.webp", optimized); - Placeholder Generation: The API supports generating 28-byte ThumbHash placeholders, which can be embedded as base64 strings in CSS to provide blurry previews while the primary image loads, avoiding additional network requests.
- Non-blocking Execution: Image processing operations run off the main thread, preventing server-side performance degradation during heavy image manipulation tasks.
Before / After
- Metadata Reads: 70 times faster than the
Sharplibrary. - Resizing Performance: Approximately 30% faster than the
Sharplibrary.
Context
Traditional Node.js image processing relies on Sharp, which depends on the libvips native binary. This dependency frequently causes installation failures in Docker and CI environments. By integrating image processing directly into the Bun runtime, the developers aim to provide a "batteries-included" experience similar to Ruby on Rails or Laravel, where common full-stack requirements like SQLite, S3, Postgres, and image manipulation are handled natively.