( data: unknown, encodedToon?: string )
| 10 | * @returns Object with toon/json sizes and whether TOON is recommended |
| 11 | */ |
| 12 | export function analyzeToonEfficiency( |
| 13 | data: unknown, |
| 14 | encodedToon?: string |
| 15 | ): { |
| 16 | toonSize: number |
| 17 | jsonSize: number |
| 18 | savingsPercent: number |
| 19 | toonRecommended: boolean |
| 20 | } { |
| 21 | const toonOutput = encodedToon ?? toonEncode(data) |
| 22 | // Compare against minified JSON for fair comparison |
| 23 | const jsonOutput = JSON.stringify(data) |
| 24 | |
| 25 | const toonSize = toonOutput.length |
| 26 | const jsonSize = jsonOutput.length |
| 27 | |
| 28 | // Calculate savings (positive = TOON is smaller) |
| 29 | // Guard against division by zero when jsonSize is 0 |
| 30 | const savingsPercent = jsonSize === 0 ? 0 : ((jsonSize - toonSize) / jsonSize) * 100 |
| 31 | |
| 32 | // TOON is recommended if it provides at least 10% savings over minified JSON |
| 33 | // Below this threshold, JSON might be preferred for compatibility |
| 34 | const toonRecommended = savingsPercent >= 10 |
| 35 | |
| 36 | return { toonSize, jsonSize, savingsPercent, toonRecommended } |
| 37 | } |
no outgoing calls
no test coverage detected