cloudflare/vinext
Publicmirrored from https://github.com/cloudflare/vinextAvailable
examples/benchmarks/app/components/format.ts
62lines · modecode
| 1 | /** Shared formatting and comparison helpers for the benchmarks dashboard. */ |
| 2 | |
| 3 | export function formatMs(ms: number | null): string { |
| 4 | if (ms === null) return "-"; |
| 5 | if (ms < 1000) return `${Math.round(ms)} ms`; |
| 6 | return `${(ms / 1000).toFixed(2)} s`; |
| 7 | } |
| 8 | |
| 9 | export function formatBytes(b: number | null): string { |
| 10 | if (b === null) return "-"; |
| 11 | if (b < 1024) return `${b} B`; |
| 12 | if (b < 1024 * 1024) return `${(b / 1024).toFixed(1)} KB`; |
| 13 | return `${(b / (1024 * 1024)).toFixed(2)} MB`; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Compare a value against a baseline where lower is better (times, sizes). |
| 18 | * Returns e.g. "2.1x faster", "1.3x slower", or null if inputs are invalid. |
| 19 | */ |
| 20 | export function speedup( |
| 21 | baseline: number | null, |
| 22 | value: number | null, |
| 23 | ): string | null { |
| 24 | if (baseline === null || value === null || baseline === 0 || value === 0) return null; |
| 25 | const ratio = baseline / value; |
| 26 | if (ratio > 1) return `${ratio.toFixed(1)}x faster`; |
| 27 | if (ratio < 1) return `${(1 / ratio).toFixed(1)}x slower`; |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Compare a size value against a baseline. |
| 33 | * Returns e.g. "56% smaller", "12% larger", or null if inputs are invalid. |
| 34 | */ |
| 35 | export function sizeReduction( |
| 36 | baseline: number | null, |
| 37 | value: number | null, |
| 38 | ): string | null { |
| 39 | if (baseline === null || value === null || baseline === 0) return null; |
| 40 | const pct = Math.round((1 - value / baseline) * 100); |
| 41 | if (pct > 0) return `${pct}% smaller`; |
| 42 | if (pct < 0) return `${Math.abs(pct)}% larger`; |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | /** True when the comparison string indicates an improvement. */ |
| 47 | export function isImprovement(label: string | null): boolean { |
| 48 | if (!label) return false; |
| 49 | return label.includes("faster") || label.includes("smaller"); |
| 50 | } |
| 51 | |
| 52 | export const RUNNER_LABELS: Record<string, string> = { |
| 53 | nextjs: "Next.js 16 (Turbopack)", |
| 54 | vinext: "vinext (Vite 7 / Rollup)", |
| 55 | vinext_rolldown: "vinext (Vite 8 / Rolldown)", |
| 56 | }; |
| 57 | |
| 58 | export const RUNNER_COLORS: Record<string, string> = { |
| 59 | nextjs: "var(--color-chart-nextjs, #f97316)", |
| 60 | vinext: "var(--color-chart-vinext, #3b82f6)", |
| 61 | vinext_rolldown: "var(--color-chart-rolldown, #8b5cf6)", |
| 62 | }; |
| 63 | |