go-twig/benchmark
semihalev e226efbbfb Optimize string to byte conversions during rendering
- Added byteBufferPool to reuse buffers during rendering
- Implemented WriteString utility function to eliminate allocations
- Updated all node Render methods to use the optimized function
- Reduced memory allocations during template rendering
- Added benchmarks showing significant improvement with io.Writer

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-11 14:16:29 +03:00
..
BENCHMARK_RESULTS.md Add serialization benchmark and update results 2025-03-11 13:58:31 +03:00
complex_comparison.go Apply gofmt formatting to improve code style 2025-03-10 11:01:47 +03:00
engine_comparison.go Add comprehensive benchmarks comparing Twig with other template engines 2025-03-10 10:32:12 +03:00
go.mod Add macro benchmark tests and update documentation 2025-03-11 13:29:17 +03:00
go.sum Add macro benchmark tests and update documentation 2025-03-11 13:29:17 +03:00
macro_benchmark.go Add macro benchmark tests and update documentation 2025-03-11 13:29:17 +03:00
memory_benchmark.go Add macro benchmark tests and update documentation 2025-03-11 13:29:17 +03:00
MEMORY_RESULTS.md Optimize string to byte conversions during rendering 2025-03-11 14:16:29 +03:00
README.md Add macro benchmark tests and update documentation 2025-03-11 13:29:17 +03:00
serialization_benchmark.go Add serialization benchmark files 2025-03-11 13:58:38 +03:00
serialization_main.go Add serialization benchmark files 2025-03-11 13:58:38 +03:00
simple_benchmark.go Apply gofmt formatting to improve code style 2025-03-10 11:01:47 +03:00

Twig Template Engine Benchmarks

This directory contains benchmark tests for comparing the Twig template engine with other popular Go template engines.

Available Benchmarks

  1. simple_benchmark.go: Basic benchmark comparing Twig with Go's standard html/template.
  2. complex_comparison.go: Comprehensive benchmark with various template complexities comparing multiple engines.
  3. engine_comparison.go: Simple comparison of multiple template engines.
  4. macro_benchmark.go: Special benchmark for testing macro functionality performance.
  5. memory_benchmark.go: Detailed memory usage comparison between Twig and Go's template.

Running the Benchmarks

To run the benchmarks:

# Basic benchmark
go run simple_benchmark.go

# Comprehensive comparison of multiple engines
go run complex_comparison.go 

# Simple engine comparison
go run engine_comparison.go

# Macro performance benchmark
go run macro_benchmark.go

# Memory usage benchmark
go run memory_benchmark.go

Benchmark Methodology

These benchmarks measure:

  1. Rendering speed: Operations per second and time per operation
  2. Memory usage: Bytes allocated per operation
  3. Allocation count: Number of memory allocations per operation

We test with various template complexities:

  • Simple templates: Basic variable substitution
  • Medium templates: Conditionals and object properties
  • Complex templates: Loops, nested conditionals, and filters
  • Macro templates: Testing macro definition, calling, and importing

Key Findings

  1. Twig consistently outperforms other interpreted template engines
  2. Twig's performance advantage increases with template complexity
  3. Twig is significantly more memory-efficient than other engines
  4. Imported macros perform slightly better than direct macros
  5. Twig is up to 2.5x faster than Go's templates for complex templates

For detailed results, see the BENCHMARK_RESULTS.md file.

Sample Results

Results will vary based on your system, but here's a sample of what to expect:

===================================================
           Template Engine Benchmark
===================================================
Go version: go1.24.1
CPU: 8 cores
GOMAXPROCS: 8
Date: 2025-03-10 15:04:05
===================================================

Running benchmarks...

BenchmarkTwig/simple:
  Operations: 500000
  Time per op: 2.5µs
  Bytes per op: 128
  Allocs per op: 2

BenchmarkGoTemplate/simple:
  Operations: 1000000
  Time per op: 1.2µs
  Bytes per op: 64
  Allocs per op: 1

...and so on for other engines and template complexities

Notes

  • The standard Go template engine is generally fastest for simple templates
  • Twig provides better syntax and features for complex templates
  • Memory usage tends to increase with template complexity
  • The benchmarks include compilation time, which affects initial rendering
  • For production use, template caching significantly improves performance