79

I would like to know your opinion about javascript template engine, which one you think is better in terms of performance?

I found some links where people do benchmarking:

http://jsperf.com/jquery-template-table-performance/15

http://jsperf.com/jquery-template-table-performance/8

http://www.viget.com/extend/benchmarking-javascript-templating-libraries/

kaha
  • 1,417
  • 2
  • 17
  • 21

5 Answers5

109

Template-Engine-Chooser! - Tool to help select the right template engine for a project.

CD..
  • 72,281
  • 25
  • 154
  • 163
  • 6
    It works so nice (sarcasm). Answering 3 first questions: Client or server: client How much logic: just basics Does it need to be one of the fastest : yes Yields no results. – Salvador Dali Mar 07 '14 at 21:18
  • 2
    @SalvadorDali -- The "fastest" all compile straight to JS, so your other requirement that it have "just the basics" conflicts since they don't limit anything. –  May 07 '14 at 01:39
  • 1
    @SalvadorDali, a tool to help you choose things won't make those things do what you want. – Will Aug 08 '14 at 23:15
  • 1
    Just noting, this site is no longer supported: https://github.com/garann/template-chooser – Chris Aug 16 '15 at 05:33
  • My favorite is Slim (http://slim-lang.com). Especially nice for ruby fans. – Alex Nazarov Apr 21 '16 at 20:36
  • 1
    This project has no support – hellboy Nov 04 '16 at 09:01
24

In terms of performance I found that it is not the templating engine itself but more if there is the possibility to precompile the templates. It is a good practice to concatenate and minify all your JavaScript source files into one file for production mode anyway, so it is basically the same step to precompile the templates, too.

I've used jQuery template and Mustache for client side templating, but my favorite is still EJS which always peformed a lot faster than anything else I tried so far, especially in production mode (compiles to native string concatenation whenever possible and needs only one DOM access to actually insert the rendered view). It is part of the JavaScriptMVC framework and when using it with StealJS as the dependency manager it does all the template compiling into production for you already (the View Engine also supports Micro, Mustache and jQuery template).

Daff
  • 43,734
  • 9
  • 106
  • 120
  • I'm currently builing with backbone.js, and it already have underscore templating, don't know if I should move to other templating engines. But what I can see in all this benchmarking mustache seems to be the winner? – kaha Oct 17 '11 at 16:25
  • I found Mustache fairly slow. Do you have actual performance problems with your templating engine? – Daff Oct 17 '11 at 16:32
  • not really, the only question I would have is, how can I precompile the templates? – kaha Oct 18 '11 at 02:28
  • Depends on the template language you decided to use. Basically you would need to execute a compiler script before minification during your build process. E.g. something like this for jQuery template https://github.com/wookiehangover/jquery-tmpl-jst – Daff Oct 18 '11 at 05:27
  • Thank you, I will see how to precompile undescore.js, if you know how to do it, please send the link here ) – kaha Oct 20 '11 at 05:20
  • I have a fork of that script that works with underscore templates here: https://github.com/kmiyashiro/jquery-tmpl-jst – kmiyashiro Nov 10 '11 at 18:56
  • 3
    EJS is the best no doubt. It simply gets out of your way and let's you code JavaScript, let's you load it remotely in a super clean way and let's you replace '<' to this '[' to work with your server-side encoding. Everything else to me is like some uber nerd trying to make it cutsy with some pretty syntax abstraction garbage. – King Friday Sep 26 '12 at 17:53
  • EJS - also my favorite - is no longer maintained, but there is a 10x higher performance fork that was created and is being maintained here: https://github.com/joshlangner/ejspeed – Joshua Jun 07 '13 at 17:34
  • This particular linked version of EJS is actually part of the CanJS framework now: http://canjs.com/docs/can.EJS.html – Daff Jun 07 '13 at 18:23
11

May be PURE — it allows to transform JSON into HTML with templates made from your existed html, not from separate template with special syntax.

From the PURE's site:

Simple and ultra-fast templating tool to generate HTML from JSON data

The representation (HTML) and the logic (JS) remain totally separated

Works standalone or with dojo, DomAssistant, Ext JS, jQuery, Mootools, Prototype, Sizzle and Sly

The best way to understand is to see what this library actually do: (from official demo) pure template engine demonstration

All the demos are here

smonff
  • 3,399
  • 3
  • 36
  • 46
Vladimir Starkov
  • 19,264
  • 8
  • 60
  • 114
  • 1
    Documentation is not that accurate and the Github repository has no issues tracker to report bugs. – smonff Feb 11 '14 at 03:40
8

LinkedIn went with dust.js http://akdubya.github.com/dustjs/

pixel 67
  • 1,530
  • 18
  • 22
  • 1
    They also explain why they choose it : http://engineering.linkedin.com/frontend/leaving-jsps-dust-moving-linkedin-dustjs-client-side-templates and http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more This is a good work and their arguments sound good to me. – Offirmo Jun 17 '13 at 14:07
4

Pick the template language that has an api and syntax you find most appealing. If you run in to performance problems, then you can look at alternatives.

Unless you are constructing tables with thousands of rows, you probably won't notice a difference.

Personally I use Google's Closure Templates. I chose it mainly because it also has a Java implementation. I have never done benchmarks.

mikerobi
  • 20,527
  • 5
  • 46
  • 42