Skip to content

Codec selection

Synkronyx Image originally encoded through sharp, a native binding to libvips. It now encodes through WebAssembly. This page records the measurements that drove that decision, so the reasoning can be checked rather than taken on trust.

Every figure below was produced on a 640 pixel wide photographic source. Reproduce them with npm run test:codecs.

sharp is fast, but it is a native module. Its binaries are platform specific, which forced a release matrix.

Native encoderWebAssembly encoder
Binary size19 MB per platformone build for all platforms
Release artefacts5 platform packages1 universal package
Runs in a browserNoYes
Extension install failure riskWrong platform package fails at runtimeNone

The deciding factor was not size. It was that a single package cannot serve every platform when the encoder is native, and that a native encoder can never run in vscode.dev, in a progressive web app, or on a phone.

LibrarySizeOutcome
wasm-vips11.9 MBRejected. Closest API match to sharp, but six times the size of the alternative.
@jsquash/*1.8 MB coreSelected. Codecs and resampling only, so pixel operations move into shared code.
photon-rsn/aRejected. Weaker format coverage.

Choosing codec-only libraries turned out to be an architectural benefit rather than a compromise. Brightness, contrast, grayscale, inversion, sepia, cropping, and blur are plain arithmetic over RGBA. They now live in @sknx/image-core as pure functions with no encoder dependency, shared by every surface instead of being expressed as encoder-specific pipeline calls.

The concern with replacing a mature native encoder is silent quality loss. Both encoders were run over the same source at three quality levels. RMSE is root mean square error against the original: lower is closer to the source.

FormatNative sizeWebAssembly sizeDifferenceRMSE nativeRMSE WebAssembly
PNG166.8 KB76.6 KB-54%3.553.92
JPEG19.1 KB18.8 KB-1%2.622.66
WebP12.1 KB12.1 KB0%2.872.87
AVIF14.8 KB12.7 KB-14%1.802.18
FormatNative sizeWebAssembly sizeDifferenceRMSE nativeRMSE WebAssembly
PNG184.0 KB81.9 KB-55%2.553.74
JPEG33.8 KB33.5 KB-1%1.181.18
WebP18.0 KB18.0 KB0%2.302.30
AVIF25.9 KB23.9 KB-8%1.121.37
FormatNative sizeWebAssembly sizeDifferenceRMSE nativeRMSE WebAssembly
PNG184.0 KB86.8 KB-53%2.553.68
JPEG45.7 KB45.7 KB0%0.520.52
WebP50.9 KB50.9 KB0%1.201.20
AVIF50.1 KB42.4 KB-15%0.590.82

WebP output is byte identical at every level, because both encoders wrap the same libwebp implementation. JPEG is identical at quality 80 and 95 once configured to match.

Neither of these was visible from a build passing or a file being produced. Both required measuring the output.

The first JPEG configuration produced output 37% larger than the native encoder at quality 95, while RMSE stayed level. Larger and marginally worse rules out a simple quality-scale difference.

Dumping the encoder defaults showed the cause:

{ "auto_subsample": true, "chroma_subsample": 2 }

auto_subsample makes mozjpeg ignore the supplied chroma setting and choose its own at high quality, dropping to 4:4:4. Setting chroma_subsample alone changed nothing, which is exactly the signature of an ignored option. Disabling auto_subsample produced identical RMSE at every quality level.

The first PNG implementation passed no options at all, producing 198 KB where the native encoder produced 184 KB. PNG output now applies the same palette budget the native encoder used below quality 100, followed by lossless optimisation through @jsquash/oxipng. That turned an 8% regression into output roughly half the size.

The higher PNG RMSE is inherent to palette quantisation and is the same trade the native encoder made with palette mode enabled. This implementation quantises more aggressively, which is where the size reduction comes from.

A 30 case matrix covering every input format against every output format passes in full.

InputPNGJPEGWebPAVIFGIF
PNGYesYesYesYesYes
JPEGYesYesYesYesYes
WebPYesYesYesYesYes
AVIFYesYesYesYesYes
GIFYesYesYesYesYes
BMPYesYesYesYesYes

BMP remains input only, matching the previous behaviour.

The BMP decoder is hand written and was validated by a stronger check than “it did not throw”. Converting a BMP to PNG produced byte identical output to converting the equivalent PNG to PNG. Those two paths share only the encoder, so identical output means the decoder is pixel exact.

Animated GIF is decoded as its first frame, which matches the previous behaviour and the warning already shown in the editor.

OperationNativeWebAssembly
2048x2048 resize, adjust, JPEG encode149 ms726 ms
640 pixel JPEG encoden/a46 ms
640 pixel AVIF encoden/a106 to 249 ms

WebAssembly is roughly five times slower in relative terms, and comfortably under a second in absolute terms. AVIF was expected to be the problem case and is not.

npm run test:codecs runs 24 tests.

Pixel operations, 13 tests. Exact channel values for brightness, contrast, all three grayscale weightings, inversion, sepia, and cropping. Resize geometry including aspect ratio locking. Validation limits for every request field, size limit parsing, and the quality retry ladder.

Codecs, 11 tests. Encode and decode for every advertised format, resize applied to encoded output verified by reading PNG header dimensions, quality affecting size, size ceilings honoured by stepping quality down, unreachable ceilings reported rather than silently exceeded, unsupported input rejected readably, placeholder dimensions and colour validation, BMP pixel exactness, BMP rejection of unsupported variants, and a GIF round trip.

These tests use only the WebAssembly encoder, so they remain valid after the native dependency is removed.