Home

Awesome

Precompiled Templates

Demo project around precompiled templates for some template engines. The goal is implement and configure things correctly, and to look for minimal footprint and best performance.

Install & build

npm install
bower install
grunt

Working demo at modules/index.html. Open browser console for rendered output.

After running grunt, the optimized build is at dist/index.html.

Numbers

Size of engine: full vs. runtime

Switching to the runtime version for precompiled templates can save lots of bytes:

_HandlebarsHoganDust
Full library (min)37764588532671
Runtime (min)441624497291
Runtime (min+gzip)187611172343

NOTE Don't compare apples and oranges: this table is only meant to show the difference between the full and runtime version of a particular engine.

Size of compiled templates

When precompiling templates from source to a JavaScript function, the size increases (this is normal: e.g. templating syntax is translated to function calls). With a small source template of 220 bytes:

_HandlebarsHoganDust
Source220220202
Compiled JS1061683671

With a larger source template of 27254 bytes:

_HandlebarsHoganDust
Source272542725426820
Compiled JS456893927433657
Increase68%44%25%

Note that the Dust source templates are slightly smaller due to its delimiter syntax ({{..}} vs. {..}).

Performance

Performance measurements are done using jsPerf: for the small source template and the larger one. These charts indicate that Handlebars is the fastest to render precompiled templates.

Conclusions

It's important to note that this is a very rough comparison between template engines using only their most basic features. When adding some complexity (e.g. helpers, partials), size and performance will change.