Performance Report: anafanafo vs pretext

Text width measurement benchmark for Shields.io badge rendering — commit 0c49f70

Summary

~2.2×
anafanafo is faster (raw speed)
< 1 px
Average width difference (Verdana)
Sub-pixel
pretext precision level
5,000
Iterations per font × 26 strings
Key Takeaway: anafanafo is approximately 2.2× faster than pretext in raw throughput. However, pretext provides sub-pixel precise measurements using real font metrics via canvas, resulting in more accurate badge rendering. The absolute time difference (~0.003 ms per call) is negligible for Shields.io's use case where badges are rendered server-side on demand.

Speed Comparison

anafanafo 2.0.0
@chenglou/pretext 0.0.3

Width Accuracy Comparison

pretext measures text using actual font outlines via the Canvas API (@napi-rs/canvas Skia backend), while anafanafo uses pre-computed character-width lookup tables. The table below shows measured widths and their differences.

Methodology

How It Works

anafanafo

Uses pre-computed character-width data stored as JSON arrays, one per supported font. Each character's width is looked up and summed to estimate the total string width. Fast but limited to the bundled font set, and returns rounded integer values.

@chenglou/pretext

A text layout library that delegates measurement to the Canvas API. In the Shields.io server environment, @napi-rs/canvas provides an OffscreenCanvas polyfill backed by Skia, giving access to real font metrics. Returns floating-point widths with sub-pixel precision.

Trade-offs

anafanafo pretext
Speed ~350 ms / 130k calls ~800 ms / 130k calls
Per-call cost ~0.003 ms ~0.006 ms
Precision Integer pixels (rounded) Sub-pixel (float)
Font support 4 bundled font/size combos Any system / loaded font
Dependencies None (pure JS + data) @napi-rs/canvas (native addon)
Accuracy Approximation from metrics tables Real font shaping via Skia
Bundle size ~45 KB (data tables) ~15 KB (JS) + native binary