Skip to content

fenom-template/benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Template Engine Benchmark

A comprehensive benchmark suite comparing the performance of popular PHP template engines: Smarty4, Twig, Fenom, and Native PHP.

Overview

This project benchmarks four PHP template solutions across three test scenarios:

  1. Echo test - Substituting 10,000 variables
  2. Foreach test - Looping through 1000 items
  3. Inheritance test - Deep template inheritance (100 levels)

Engines Tested

Engine Version Description
Smarty4 4.5.6 PHP Template Engine (LGPL-3.0)
Twig 3.14+ The flexible, fast, and secure template language
Fenom 3.1.0 Excellent template engine for PHP (BSD-3)
Native PHP - Plain PHP with extract() and output buffering

Requirements

  • PHP >= 7.4 (tested up to PHP 8.4)
  • Composer

Installation

composer install

Running Benchmarks

php bin/fenom-benchmark.php

Options

  • --stress N - Run each template N times for stress testing
  • --auto_reload - Enable auto-reload mode (recompile on change)
  • --cleanup - Clean up compiled templates after benchmark

Example

php bin/fenom-benchmark.php --stress 10 --cleanup

Running Tests

./vendor/bin/phpunit

Or with coverage:

./vendor/bin/phpunit --coverage-html coverage

Benchmark Results

Test Environment

  • Platform: macOS (Darwin)
  • PHP Version: 8.4
  • Date: April 2026

Test 1: Echo Variable Substitution (10,000 variables)

Engine !compiled !loaded compiled !loaded compiled loaded
native 0.0002 sec 0.0002 sec 0.0002 sec
fenom 0.0210 sec 0.0036 sec 0.0002 sec
smarty4 0.0654 sec 0.0048 sec 0.0003 sec
twig 0.0736 sec 0.0056 sec 0.0005 sec

Memory usage:

  • native: 2.2 MiB
  • fenom: 8.2-9.2 MiB
  • smarty4: 10.0-19.1 MiB
  • twig: 9.3-32.6 MiB

Test 2: Foreach Loop (1000 items, 10 fields each)

Engine !compiled !loaded compiled !loaded compiled loaded
native 0.0002 sec 0.0002 sec 0.0002 sec
fenom 0.0019 sec 0.0004 sec 0.0003 sec
smarty4 0.0038 sec 0.0006 sec 0.0003 sec
twig 0.0057 sec 0.0015 sec 0.0012 sec

Memory usage:

  • native: 2.0 MiB
  • fenom: 2.2-2.9 MiB
  • smarty4: 2.5-4.2 MiB
  • twig: 2.9-4.1 MiB

Test 3: Deep Template Inheritance (100 levels)

Engine !compiled !loaded compiled !loaded compiled loaded
native N/A N/A N/A
fenom 0.0062 sec 0.0001 sec 0.0000 sec
smarty4 0.0382 sec 0.0043 sec 0.0035 sec
twig 0.0365 sec 0.0051 sec 0.0002 sec

Memory usage:

  • fenom: 2.2-3.7 MiB
  • smarty4: 6.6-8.6 MiB
  • twig: 6.8-9.1 MiB

Note: Native PHP does not support template inheritance natively.


Summary

Rank Engine First Run Cached (loaded) Memory
1 Native PHP 0.0002s 0.0002s ~2 MiB
2 Fenom 0.0210s 0.0002s ~8 MiB
3 Smarty4 0.0654s 0.0003s ~10 MiB
4 Twig 0.0736s 0.0005s ~10-32 MiB

Key Findings

  1. Native PHP is fastest - but lacks features like template inheritance, caching, and security escaping
  2. Fenom is fastest among full-featured engines - excellent for high-traffic sites
  3. Smarty4 and Twig are comparable - Smarty4 has better first-run performance, Twig has better cached performance for inheritance
  4. All engines show significant caching benefits - first-run is 100-300x slower than cached

Project Structure

benchmark/
├── bin/
│   ├── fenom-benchmark.php    # Main benchmark runner
│   └── engine.php             # Engine launcher
├── compile/                    # Compiled templates cache
├── src/
│   └── Benchmark.php           # Benchmark class
├── templates/
│   ├── echo/                   # Echo variable test
│   ├── foreach/                # Foreach loop test
│   └── inheritance/            # Template inheritance test
├── tests/
│   └── BenchmarkTest.php      # PHPUnit tests
├── composer.json
├── phpunit.xml
├── README.md
└── README_ru.md

License

BSD-3-Clause

Authors

  • Ivan Shalganov
  • Dmitry Patsura
  • Maxim Kostjukevich

About

Template Engine Benchmark

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors