What steps can be taken to improve jade template rendering performance in express using nodejs

我与影子孤独终老i 提交于 2019-12-04 16:45:11

问题


Background

jade syntax is awesome but i wanted to see how it was affecting performance.

So i created a single page app and used apache bench to compare its throughput using jade to render a page vs using an in memory string. There were no variables so this was a purely academic comparison.

The in memory string made the entire app more than twice as fast locally, which seems a lot considering jade in production mode should be rendering from an in memory cache.

I'm using node 0.8 and the version 2.5.11 of express in production mode with the view cache option explicitly set to true.

apache bench results: https://dl.dropbox.com/u/3737990/jade/jade.png https://dl.dropbox.com/u/3737990/jade/memory.png


回答1:


As mentioned by Harry, it's meaningless to compare a template engine's performance to the performance of sending a string, since those address two different needs. It's somewhat like comparing the MPG of two cars, except one car you just put into neutral and let it roll down a hill.

Instead, it is much more helpful to compare templating engines, since they are all means to the same ends (dynamically rendered HTML).

Here we see that Jade is the slowest templating language. There are probably a lot of factors that play in to why this is the case, but the core issue is that Jade wasn't designed for speed. If you need extremely high performance, doT was designed for speed.




回答2:


An in-memory string is the absolute fastest thing you can do, so comparing against it is not very meaningful. A template is never going to be as fast as string concat. Setting to production mode is the most important thing you can do performance wise.




回答3:


(Adding this extra bit of information since this seems to be one of the first search engine hits when looking for "express jade performance")

I had the same issue with a nodejs production application. The problem is that jade runs by default on development mode which is not what you want for production, since this will recompile all templates again and again, wasting cpu and memory.

The solution is to run your app with: NODE_ENV=production node app.js, this will prevent jade recompiling cycle, and perhaps trigger some other express perf improvements.

Note that this doesn't make jade faster, it just prevents it from doing unnecessary work and killing your CPU.



来源:https://stackoverflow.com/questions/11343302/what-steps-can-be-taken-to-improve-jade-template-rendering-performance-in-expres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!