Performance Benchmarks
Last updated: 2024-09-24
Overview
This benchmark compares the performance of THT versus the most popular PHP framework: Laravel.
Why compare a language to a framework?
1) THT takes a “batteries included” approach by providing essential web framework features.
2) Some might be worried that running a transpiled language would be too slow to run in production, and Laravel is arguably the best example of a production-ready PHP framework.
Task
- Take a route parameter from the URL.
- Loop through a list of 100 sub-templates using incremented values.
// URL /benchmark/route/1234
What does this test?
- The overhead of running the framework or language
- URL routing
- Template system
The Environment
- PHP 8.3 (OPcache enabled)
- MacBook Pro 2021 (M1, 32 GB)
- MacOS Sonoma 14.4.1
- Apache/2.4.58 (Unix)
Benchmark Runner
- Apache AB utility
- Requests: 1,000
- Concurrency: 10 requests
- KeepAlive: true
ab -k -c 10 -n 1000 http://localhost:8080/benchmark/route/1234
Benchmark Results
Framework | Requests/sec | (More = Faster) |
---|---|---|
Laravel | 352 | |
THT | 5,188 |
Takeaway
THT performed significantly better than Laravel.
If you’re interested in using THT, you can be reasonably confident that it will be fast enough for most use cases.
Benchmark Code
Laravel
- Laravel v11
- Default installation
- Ran
php artisan optimize
file: routes/web.php
<?php use Illuminate\Support\Facades\Route; Route::get('/benchmark/route/{pid}', function () { $pid = Route::current()->parameter('pid'); for ($i = 1; $i <= 100; $i += 1) { $item = [ 'num' => $i, 'name' => $pid . $i, ]; $items[] = $item; } return view('welcome', ['pid' => $pid, 'items' => $items]); });
file: resources/views/welcome.blade.php
<!doctype html> <html> <head> <title>Route Param Test</title> </head> <body> LAR - Route Param: {{ $pid }} <ul> @foreach ($items as $item) @include('item', $item) @endforeach </ul> </body> </html>
file: resources/views/item.blade.php
<li>{{ $item['num'] }}: {{ $item['name'] }}</li>
THT
- THT v0.8 - Beta
- Default installation
file: config/app.jcon
{ routes: { /benchmark/route/{pid}: /test-route.tht } }
file: pages/test-route.tht
fun main { $pid = Input.route('pid') $items = [] foreach range(1, 100) as $i { $item = { num: $i name: $pid ~ $i } $items #= $item } Output.sendHtml(pageHtml($pid, $items)) } tem pageHtml($pid, $items) { <!doctype html> <html> <head> <title>Route Param Test</> </> <body> THT - Route Param: {{ $pid }} <ul> --- foreach $items as $item { {{ itemHtml($item) }} --- } </> </> </> } tem itemHtml($item) { <li>{{ $item.num }}: {{ $item.name }}</> }