Skip to content

Benchmarks

All benchmarks run on Mac Studio M1 Max, Bun 1.3.5.

Two Rendering Modes

Runtime Mode

render() - Best for development and templates with dynamic inheritance. 2-4x faster than Nunjucks.

AOT Mode

compile() - Pre-compile templates to JavaScript functions. 160x faster than Nunjucks.

Runtime Performance vs Nunjucks

BenchmarkbinjaNunjucksSpeedup
Simple Template371K ops/s96K ops/s3.9x
Complex Template44K ops/s23K ops/s2.0x
Multiple Filters246K ops/s63K ops/s3.9x
Nested Loops76K ops/s26K ops/s3.0x
Conditionals84K ops/s25K ops/s3.4x
HTML Escaping985K ops/s242K ops/s4.1x
Large Dataset9.6K ops/s6.6K ops/s1.5x

AOT Compilation Performance

AOT compilation provides massive speedups by pre-compiling templates to optimized JavaScript functions:

Benchmarkbinja AOTbinja RuntimeSpeedup
Simple Template14.3M ops/s371K ops/s39x
Complex Template1.07M ops/s44K ops/s24x
Nested Loops1.75M ops/s76K ops/s23x

vs Nunjucks

Benchmarkbinja AOTNunjucksSpeedup
Simple Template14.3M ops/s96K ops/s160x
Complex Template1.07M ops/s23K ops/s46x

Why binja is Faster

1. Pure TypeScript for Bun

binja is built from scratch in TypeScript, optimized specifically for the Bun runtime. No legacy code or Node.js compatibility layers.

2. Inline Filter Optimization

~70 common filters have inline implementations to avoid function call overhead:

// Instead of:
filters['upper'](value)
// binja does:
value.toUpperCase()

This provides 10-15% speedup on filter-heavy templates.

3. AOT Compilation

Pre-compiling templates eliminates:

  • Lexing overhead
  • Parsing overhead
  • AST traversal
  • Filter lookup
// Compile once at startup
const template = compile('<h1>{{ title|upper }}</h1>')
// Renders in microseconds
const html = template({ title: 'hello' })

4. Efficient AST Representation

binja’s AST is optimized for fast traversal and minimal memory allocation.

Running Benchmarks

Terminal window
# Clone the repo
git clone https://github.com/egeominotti/binja.git
cd binja
# Install dependencies
bun install
# Run benchmarks
bun run benchmark

Production Recommendations

  1. Use AOT in Production - compile() for static templates
  2. Pre-compile at Startup - Compile templates once, use many times
  3. Enable Caching - Use cache: true in Environment
  4. Monitor Cache - Use env.cacheStats() to optimize cacheMaxSize
import { compile } from 'binja'
// Best practice: compile at startup
const templates = {
home: compile(await Bun.file('./views/home.html').text()),
user: compile(await Bun.file('./views/user.html').text()),
}
// Sync rendering, extremely fast
app.get('/', () => templates.home({ title: 'Home' }))