Choosing a template engine for the Comet project , I decided to compare all the engines popular in the PHP community. Usually this choice is dictated by the framework: the symphonist will wrap the templates in Twig, the Laravel programmer will arm himself with Blade. But I was interested in the question - how do these options differ in terms of performance? After testing seven engines and pure PHP, I got an answer. Data, schedules, champions and losers - under the cut!
He recalled the names of the top applicants; he found the rest, armed with GitHub statistics and discussions on Reddit. Here is the list:Smarty: github.com/smarty-php/smartyPlates: github.com/thephpleague/platesMustache: github.com/bobthecow/mustache.phpTwig: github.com/twigphp/TwigBlade: github.com / jenssegers / bladeBladeOne: github.com/EFTEC/BladeOneLatte: github.com/nette/latteIf you know an interesting option - write, I will add to the test. Blade is pretty deeply integrated into Laravel, so I had to take a couple of its standalone implementations. Unfortunately, none of them supports Blade-X components.To understand the essence of the benchmark, the easiest way is to look at the version of code with pure PHP:$data = [
(object) [
"code" => 200,
"message" => "OK"
],
(object) [
"code" => 404,
"message" => "Not Found"
],
(object) [
"code" => 500,
"message" => "Internal Server Error"
],
];
$html = '<html><head></head><body>';
foreach ($data as $message) {
$html .= "<p>$message->code : $message->message</p>";
}
$html .= '</body></html>';
This is a synthetic test for outputting an array of three objects into an HTML template containing two properties: HTML code and its brief description.This is what the analog on Twig looks like:<html><head></head><body>
{% for message in data %}
<p>{{ message.code }} : {{ message.message }}</p>
{% endfor %}
</body></html>
And this is Blade:<html><head></head><body>
@foreach ($data as $message)
<p>{{ $message->code }} : {{ $message->message }}</p>
@endforeach
</body></html>
The tests were run in the Ubuntu 20.04 / PHP 7.4 / Comet 0.6 container on a virtual machine with 4 Ryzen 3600 cores and 4G memory:wrk --connections=500 --threads=2 --duration=10s http:
It turned out this alignment, each graph reflects the average number of requests successfully completed in one second:
Pure PHP - expected first, but unexpectedly, that Blade is already half as much behind! And why is the βlightweightβ Plates lagging behind the βpowerfulβ Twig? All frameworks use regular caching, so the results are as close as possible to real ones.Within the framework of one article, I did not want to overload the reader with an extended analysis of the code, work strategies and caching of engines, analysis of criteria for choosing a template engine that are important to me. If the topic is interesting - I will write a sequel.As an advertisement: look at Comet , in the near future - to make it the fastest and most convenient PHP framework for creating restful APIs and microservices :)