Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swig or Nunjucks? #179

Closed
kokujin opened this issue Jan 19, 2014 · 15 comments
Closed

Swig or Nunjucks? #179

kokujin opened this issue Jan 19, 2014 · 15 comments

Comments

@kokujin
Copy link

kokujin commented Jan 19, 2014

I am evaluating a templating engine along the lines of the Django/Jinja templating engines. How does Nunjucks stack against Swig?

  1. Speed
  2. Client side loading as AMD module
  3. I did not find any CLI version of Nunjucks, does one exist?

Here is an overview which might help:

http://paularmstrong.github.io/node-templates/

Thanks

@mkoryak
Copy link

mkoryak commented Jan 19, 2014

This has come up before:

https://github.com/jlongster/nunjucks/issues/83

@kokujin
Copy link
Author

kokujin commented Jan 20, 2014

Thanks for the link mkoryak, I did not see any benchmarks though. What about speed?

@popomore
Copy link
Contributor

Nunjucks has bench, but seems no compare with swig.

https://github.com/jlongster/nunjucks/tree/master/bench

@popomore
Copy link
Contributor

Hi @jlongster, I write some code to compare nunjucks with swig, would you like review it?

I don't know why nunjuck is faster than swig when render file, however the opposite result when render string.

https://github.com/popomore/nunjucks-vs-swig

@jlongster
Copy link
Contributor

@popomore You're benchmark looks good. Nunjucks is fast when using file because the template is cached in between renders, and the code it generates is really fast. So you're basically just executing some JavaScript when rending a template. Rendering straight from a string each time is a lot slower because it hits the compiler and needs to be compiled to JS every single time. Additionally, since a new JS function is being run each time, the compiler never has a chance to trace and optimize it internally.

This is a good example of why it's crazy to focus on optimizing the compiler, when you really just need to optimize the generated code and make sure you have a good caching strategy. For client-side browsers, this means a good strategy for using precompiled templates, which nunjucks has and swig doesn't.

I see that you configured swig to cache the file template in memory, so I'm not sure why it is much slower even when caching the compiled code.

@jlongster
Copy link
Contributor

Also note that you shouldn't spend too much time benchmarking templates -- most of the time it is not the bottleneck. But it's nice to see nunjucks perform so well.

@jlongster
Copy link
Contributor

@kokujin

  1. See above benchmarks. Nunjucks is about as fast as it can be (it compiles to raw JavaScript so you're just executing bits of JS, it's really fast). If you're using them client-side, just make sure to precompile your templates in production.
  2. See http://jlongster.github.io/nunjucks/getting-started.html#which-file-should-you-use. You can load it as an AMD module.
  3. There is a CLI tool called nunjucks-precompile which lets you compile stuff to a JS blob. http://jlongster.github.io/nunjucks/api.html#precompiling

@popomore
Copy link
Contributor

Thanks James, I have another question that can I use precompile in server side and is it recommended?—
Sent from Mailbox for iPhone

On Wed, Jan 22, 2014 at 11:56 PM, James Long notifications@github.com
wrote:

Closed #179.

Reply to this email directly or view it on GitHub:
https://github.com/jlongster/nunjucks/issues/179

@jlongster
Copy link
Contributor

No, you shouldn't use precompiled templates on the server-side (I think it even references window). By default, nunjucks caches templates on the server-side since the same nunjucks instance is used across requests, so everything should just work.

@popomore
Copy link
Contributor

Thanks for your advise.

@apfelbox
Copy link

About benchmarks, just wanted to post this link here: http://jsperf.com/dot-nunjucks-mustache/11
I don't exactly advise synthetic benchmarks and agree with @jlongster that fixing performance problems comes after improving the life of your developer - but it gives a good overview.

@mkoryak
Copy link

mkoryak commented Oct 17, 2014

@apfelbox cool benchmark, but unless performance is your number one problem, then I wouldn't select a framework based on its performance ;)

@jlongster
Copy link
Contributor

Yeah, the gist is that it's competitive so we're ok. Precompiled templates are the important benchmark here, and that benchmark varies quite a bit between browsers. On average precompiled nunjucks beats or meets all the other ones except doT (which does seem to be a lot faster, but I don't know anything about it, it's probably a lot simpler).

On my computer in Firefox nightly nunjucks precompiled comes in at 11,505, while dust.js precompiled is 12,291 and swig precompiled is only 2,134.

@apfelbox
Copy link

@mkoryak I agree completely. I just posted the link for completeness sake.

Personally I first look for things like security, ease of use, available features and difficulty to extend the system. On the other hand you don't need to waste CPU cycles unnecessarily.

So you need to find a system which fulfills all of your requirements and is sufficiently fast. I just found out about nunjucks today, but it seems to fit this description.

edit
@jlongster doT is in the large part string replacements + eval at the end. You can't beat that performance wise. But maybe you don't want to, because your template engine needs to do some things more or provide more comfort.

@lichenhao
Copy link

I was wrote a case a few time ago.

https://jsfiddle.net/cLkjp99f/9/

The swig.js is best choice for precompile or in browser side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants