Skip to content

Comparison with Twig

Jorge Castro edited this page May 8, 2020 · 6 revisions

Comparison with Twig (May 2020)

First Time Time First Time Memory Overhead First Time Second Time Second Time Memory
BladeOne 1962ms 2024kb 263 1917ms 2024kb
Twig 3734ms 2564kb 123 3604ms 2327kb
PHP Raw 150ms 927kb 0 150ms 927kb

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 concurrents calls in less than a second, so commonly the performance is acceptable for both cases.

What is the overhead first time?: It is the comparison between the call to the code and the execution of the code. In theory, the Time - overhead time = Time of PHP vanilla code. It is only for reference because both libraries generate different PHP code.

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>

And it is PHP raw

<ul>
<?php 
foreach($categories as $category) {  ?>
<li><?php echo \htmlentities($category, ENT_QUOTES, 'UTF-8', false); ?></li>
<?php } ?>
</ul>

Compiled

Twig compiled looks like:

<?php

use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Extension\SandboxExtension;
use Twig\Markup;
use Twig\Sandbox\SecurityError;
use Twig\Sandbox\SecurityNotAllowedTagError;
use Twig\Sandbox\SecurityNotAllowedFilterError;
use Twig\Sandbox\SecurityNotAllowedFunctionError;
use Twig\Source;
use Twig\Template;

/* index.php */
class __TwigTemplate_91280ddd9da06f8a99b4be752a69607598d88a074bb0a7655eeb74e472a4c689 extends Template
{
    private $source;
    private $macros = [];

    public function __construct(Environment $env)
    {
        parent::__construct($env);

        $this->source = $this->getSourceContext();

        $this->parent = false;

        $this->blocks = [
        ];
    }

    protected function doDisplay(array $context, array $blocks = [])
    {
        $macros = $this->macros;
        // line 1
        echo "<ul>
    ";
        // line 2
        $context['_parent'] = $context;
        $context['_seq'] = twig_ensure_traversable(($context["categories"] ?? null));
        foreach ($context['_seq'] as $context["_key"] => $context["category"]) {
            // line 3
            echo "        <li>";
            echo twig_escape_filter($this->env, $context["category"], "html", null, true);
            echo "</li>
    ";
        }
        $_parent = $context['_parent'];
        unset($context['_seq'], $context['_iterated'], $context['_key'], $context['category'], $context['_parent'], $context['loop']);
        $context = array_intersect_key($context, $_parent) + $_parent;
        // line 5
        echo "</ul>
";
    }

    public function getTemplateName()
    {
        return "index.php";
    }

    public function isTraitable()
    {
        return false;
    }

    public function getDebugInfo()
    {
        return array (  53 => 5,  44 => 3,  40 => 2,  37 => 1,);
    }

    public function getSourceContext()
    {
        return new Source("", "index.php", "D:\\folder\\www\\twig-test\\views\\index.php");
    }
}

While BladeOne compiled is :

<ul>
 <?php $__currentLoopData = $categories; $this->addLoop($__currentLoopData);$this->getFirstLoop();
 foreach($__currentLoopData as $category): $loop = $this->incrementLoopIndices();  ?>
 <li><?php echo \htmlentities($category, ENT_QUOTES, 'UTF-8', false); ?></li>
 <?php endforeach; $this->popLoop(); $loop = $this->getFirstLoop(); ?>
</ul>

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.