This directory contains benchmarking tools for evaluating the token efficiency of the TOON (Token-Oriented Object Notation) format compared to JSON and XML.
The benchmarks measure how effectively TOON reduces token consumption when encoding structured data, which is crucial for minimizing costs and latency when working with Large Language Models (LLMs).
cd benchmarks
composer installThis benchmark compares TOON, JSON, and XML token counts across multiple realistic datasets:
composer run benchmarkOr directly:
php scripts/token-efficiency.phpThe benchmark supports two token counting methods:
- Anthropic API (Recommended) - Uses Claude's actual tokenizer for accurate counts
- Estimation (Fallback) - Character/word-based estimation when API is unavailable
To use the Anthropic API method:
-
Copy
.env.exampleto.env:cp .env.example .env
-
Add your Anthropic API key to
.env:ANTHROPIC_API_KEY=your_api_key_here
Results are saved to results/token-efficiency.md and include:
- Token counts for each format (TOON, JSON, XML)
- Percentage savings for TOON compared to other formats
- Visual progress bars showing relative token usage
- Summary statistics across all benchmarks
The benchmark evaluates four different types of structured data:
- GitHub Repositories (100 records) - Repository metadata with stars, forks, etc.
- Analytics Data (180 days) - Time-series web metrics with views, clicks, conversions
- E-Commerce Orders (50 orders) - Nested order data with customers and items
- Employee Records (100 records) - Tabular employee data
All datasets are generated using Faker PHP with seeded randomization for reproducibility.
benchmarks/
├── src/
│ ├── Datasets.php # Dataset generators
│ ├── Formatters.php # TOON, JSON, XML formatters
│ ├── TokenCounter.php # Token counting utilities
│ └── Report.php # Markdown report generator
├── scripts/
│ └── token-efficiency.php # Main benchmark script
├── data/ # Generated test data (if needed)
└── results/ # Benchmark output reports
Add new dataset generators to src/Datasets.php:
public function generateCustomData(): array
{
// Your dataset generation logic
return $data;
}Then add the dataset to the benchmarks array in scripts/token-efficiency.php:
$benchmarks[] = [
'name' => 'Custom Dataset',
'description' => 'Description of what this tests',
'data' => fn() => $datasets->generateCustomData(),
];Implement formatters in src/Formatters.php:
public static function toCustomFormat(array $data): string
{
// Your formatting logic
}╔════════════════════════════════════════════════════════════════╗
║ TOON Token Efficiency Benchmark (PHP) ║
╚════════════════════════════════════════════════════════════════╝
Token Counting Method: Anthropic API
[1/4] Running: GitHub Repositories...
→ Formatting data... ✓
→ Counting tokens... ✓
→ Results:
TOON: 3,346 tokens
JSON: 6,276 tokens
XML: 8,673 tokens
→ TOON saves 46.7% vs JSON, 61.4% vs XML
...
╔════════════════════════════════════════════════════════════════╗
║ Benchmark Complete! 🎉 ║
╚════════════════════════════════════════════════════════════════╝
- PHP 8.1 or higher
- Composer
- (Optional) Anthropic API key for accurate token counting
MIT - Same as the parent TOON library