Technical comparison between Blade and Twig

Jorge Castro
Cook php
Published in
2 min readMay 8, 2020

What it is about?

Both are template libraries for PHP. It helps to develop fast websites while it keeps the code tidy and organized.

What it was tested?. It was tested two features (that are the most used):

  • Show a value (escaped)
  • List an array

It was tested with an array with 10k elements and tested many times.

BladeOne is fastest.

BladeOne (at least for this exercise) is practically the double as fast as Twig. Why? I will explain it later

What was tested?. It was tested two features (that are the most used): It was tested with an array with 10k elements and tested many times.

Versions used:

"require": {
"twig/twig": "^3.0.3",
"eftec/bladeone": "^3.44"
},

Twig ran with auto_reload=false; (default value and fast) BladeOne ran with BladeOne::MODE_AUTO; (default value and fast but its not the fastest).

Tested on PHP 7.4, Windows 64 bits, and SSD.

Note: In my testing machine, I can run 150 concurrent calls in less than a second, so commonly the performance is acceptable for both cases. Both libraries are blazing fast and It could hold a high performance site.

Templates of the example

In the example, the code was repeated many times, it also added some long HTML. Here it is the resumed version:

Twig Template:

<ul>
{% for category in categories %}
<li>{{ category }}</li>
{% endfor %}
</ul>

BladeOne Template:

<ul>
@foreach($categories as $category)
<li>{{ $category }}</li>
@endforeach
</ul>

Compiled

Both works with the next ideology. The view is converted or compiled into a native PHP code. But it is the only similarities. Let’s check the differences:

Twig compiled looks like:

While BladeOne compiled is :

They are quite different but they do the same.

Conclusion.

Twig allows more features than BladeOne, but it sacrifices the performance. BladeOne generates practically vanilla code, while Twig generates a class.
While both libraries do the same but they use different strategies.

--

--