Benchmark of Elefant's template engine against Twig templates

Overview

Template Benchmark

This is a simple benchmark to test the memory usage and speed of rendering templates using Elefant's Template engine, Twig, and Smarty 3.

Machine specs

The tests were done on an iMac with the following specs:

Mac OS X 10.7.4
3.06 GHz Core 2 Duo w/ 8 GB RAM
Apache 2.2.21
PHP 5.3.10

Running the tests

To run the tests yourself, started with a fresh copy of Elefant 1.3.1-beta. Add the following composer.json file to the root of the site:

{
	"require": {
		"twig/twig": "1.*"
	},
	"config": {
		"vendor-dir": "lib/vendor"
	}
}

Now run the following to install Twig:

cd /path/to/your/site
php composer.phar install

Note: Requires Composer.

Lastly, run these commands to install the tests:

cd apps
git clone https://github.com/jbroadway/template-bench.git

This adds the following new URLs to Elefant:

The first time each of these is run, it will generate the cached copy of the template. Subsequent runs will use the cached templates.

Rendering with compilation (uncached)

Engine Microtime Memory
Raw PHP 0.00012397766113281 7 KB
Elefant 0.00062322616577148 60 KB
Smarty 3 0.033155918121338 4.7 MB
Twig 0.051060914993286 2.9 MB

In the compilation step, which runs whenever a template file has changed, Elefant uses negligible memory compared to Twig's 2.9 MB and Smarty's 4.7 MB, and generates the cached template in a fraction of the time.

Rendering from cached template

Engine Microtime Memory
Raw PHP 0.00012397766113281 7 KB
Elefant 0.00019192695617676 54 KB
Smarty 3 0.00063490867614746 8 KB
Twig 0.0015380382537842 248 KB

Rendering from cached templates puts Elefant at 8x faster and using 1/5th of the memory as Twig does, and about 3x faster than Smarty. Interestingly, regenerating templates in Elefant uses only 6 KB more memory than rendering from cache, and is also faster than Twig is at rendering a template from cache, and equal in speed to Smarty's compiled performance.

Setup memory usage

I also wanted to see how much memory the setup for each library required. For this, I went outside of the Elefant framework in order to load each library independently of the framework surrounding it. Here are the results of how much memory each templating engine consumes during its setup:

Engine Memory
Raw PHP 0 KB
Elefant 375 KB
Smarty 3 1.7 MB
Twig 851 KB

The results clearly show Elefant leading in memory usage and speed, with Smarty 3 a close second on performance, and Twig trailing behind on rendering but still using less overall memory than Smarty 3.

Notes

The raw PHP test added a sanitizing function, since that is such an important consideration in data output. This function simply wraps htmlspecialchars() with the appropriate parameters.

You might also like...
Manifest is a ProcessWire module that bridges between Twig and Webpack.

Manifest is a ProcessWire module that bridges between Twig and Webpack.

Twig extensions for common navigation widgets for symfony.

Twig Navigation Extension This bundle provides a Twig extensions for common navigation widgets. Installation composer require m2mtech/twig-navigation-

Pattern Lab Standard Edition for Twig

Pattern Lab Standard Edition for Twig The Standard Edition for Twig gives developers and designers a clean and stable base from which to develop a Twi

Intl support for Twig

Twig Intl Extension This package is a Twig extension that provides the following: country_name filter: returns the country name given its two-letter/f

A custom twig extension to truncate text while preserving HTML tags.

TwigTruncateExtension A custom twig extension to truncate text while preserving HTML tags. Installation Add the library to your app's composer.json:

Redaxo 5-Addon zum gruppieren beliebiger Inhaltsmodule (Blöcke) innerhalb eines Spaltenrasters mit selbst definierten Templates.
Redaxo 5-Addon zum gruppieren beliebiger Inhaltsmodule (Blöcke) innerhalb eines Spaltenrasters mit selbst definierten Templates.

Gridblock Redaxo 5-Addon zum gruppieren beliebiger Inhaltsmodule (Blöcke) innerhalb eines Spaltenrasters. Die Inhaltsmodule entsprechen dabei den übli

A simple plugin to override all woocommerce templates

A simple plugin to override all woocommerce templates

Rah memcached - Store parts of Textpattern CMS templates in Memcached

rah_memcached Packagist | Issues | Donate A plugin for Textpattern CMS that stores parts of your templates in Memcached, a distributed in-memory key-v

Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates
Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates

Magento 2 Image Resizer Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates Installation $ composer require

Comments
  • Too simple templates

    Too simple templates

    This benchmark could be very interesting if it tested larger templates. E.g the template could render a typical github repo tree, or other pages people can relate too.

    opened by Zakay 1
Owner
John de Plume
Full-stack + VR developer. Singer-songwriter/one-man synthpop band. CTO/Co-Founder @ HeyAlfa + Flipside XR.
John de Plume
PaaS template based on production template using platform.sh

Shopware for Platform.sh This template builds Shopware on Platform.sh using Composer. To get started on Platform.sh, please visit https://docs.platfor

Shopware 9 Oct 12, 2022
PDF API. JSON to PDF. PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data

PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data PDF ENGINE VERSION: development: This is a prerelease version

Ajous Solutions 2 Dec 30, 2022
The easiest way to match data structures like JSON/PlainText/XML against readable patterns. Sandbox:

PHP Matcher Library created for testing all kinds of JSON/XML/TXT/Scalar values against patterns. API: PHPMatcher::match($value = '{"foo": "bar"}', $p

Coduo 774 Dec 31, 2022
A Magento implementation for validating JSON Structures against a given Schema

Zepgram JsonSchema A Magento implementation for validating JSON Structures against a given Schema with support for Schemas of Draft-3 or Draft-4. Base

Benjamin Calef 1 Nov 5, 2021
The SensioLabs DeprecationDetector runs a static code analysis against your project's source code to find usages of deprecated methods, classes and interfaces

SensioLabs DeprecationDetector CAUTION: This package is abandoned and will no longer receive any updates. The SensioLabs DeprecationDetector runs a st

QOSSMIC GmbH 389 Nov 24, 2022
Snuffleupagus is a PHP 7+ and 8+ module designed to drastically raise the cost of attacks against websites, by killing entire bug classes

Snuffleupagus is a PHP 7+ and 8+ module designed to drastically raise the cost of attacks against websites, by killing entire bug classes

Julien Voisin 625 Jan 3, 2023
LendCash is a cash lending service that lets you take loans against your stocks portfolio value and pay back on a prorated basis.

LendCash is a cash lending service that lets you take loans against your stocks portfolio value and pay back on a prorated basis.

Teniola Fatunmbi 2 Aug 22, 2022
A small PHP Script to protect your site against DDoS attack .

Anti-DDoS A small PHP Script to protect your site against DDoS attack. Description Most of bots can't execute JavaScript code or can execute code part

Arman Msv 3 Dec 20, 2022
Dobren Dragojević 6 Jun 11, 2023
A tool to automatically fix Twig Coding Standards issues

Twig CS Fixer Installation This standard can be installed with the Composer dependency manager. Add the coding standard as a dependency of your projec

Vincent Langlet 50 Jan 6, 2023