svag
An SVG minifier.
Features
- Remove XML declarations, DOCTYPE, comments
- Remove metadata, title, desc elements
- Remove Inkscape/Sodipodi namespaces and elements
- Remove unused namespace declarations
- Collapse unnecessary groups
- Remove hidden and empty elements
- Minify path data (reduce precision, implicit commands)
- Minify colors (
#ff0000→red,#ffffff→#fff) - Remove default attribute values
- Minify inline styles
- Sort attributes for better gzip
Usage
As a library
use minify;
let svg = r#"<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<!-- A red square -->
<rect x="10" y="10" width="80" height="80" fill="#ff0000" fill-opacity="1"/>
</svg>"#;
let minified = minify.unwrap;
// <svg xmlns="http://www.w3.org/2000/svg" height="100" width="100"><rect fill="red" height="80" width="80" x="10" y="10"/></svg>
As a CLI
# From stdin
|
# From file
# With stats
# 1961 -> 602 bytes (69.3% smaller)
With custom options
use ;
let options = Options ;
let minified = minify_with_options.unwrap;
Installation
Or for the CLI:
Benchmarks
Test corpus: 4858 SVG files (1042.5 MB total)
| svag | svgo | |
|---|---|---|
| Output size | 729.0 MB (-30.1%) | 412.2 MB (-60.5%) |
| Bytes saved | 313.6 MB | 630.3 MB |
| Processing time | 514.1ms | 26.92s |
The test corpus includes SVGs from the W3C SVG 1.1 Test Suite, KDE Oxygen Icons, and Wikimedia Commons. Duplicates are removed by content hash.
Both tools run in parallel using all available CPU cores:
- svag: Rust with rayon, release build
- svgo: Node.js with worker_threads
Timing is wall-clock time for processing all files. This avoids penalizing svgo for Node.js startup overhead.
To regenerate: npm install svgo && cargo xtask fetch-corpus && cargo xtask readme
FAQ
Why "svag"?
Because it's swag.
Is it production-ready?
No, but the tests make me reasonably sure it won't mess anything up.
Roadmap
- More optimization passes (close the gap with svgo's compression)
Inspired by
License
MIT OR Apache-2.0