Nodejs Javascript模板引擎性能对比及几点优化
Nodejs Javascript模板引擎性能对比及几点优化
浏览器版可直接查看: JavaScript template language shootoff
运行性能测试
测试代码在:js-template-benchmarks
$ node benchmarks.js
我的测试环境
CPU: 4核 Intel(R) Core(TM) i3 CPU M 330 @ 2.13GHz
Memory: 4GB
OS: Ubuntu 10.10 2.6.35-28-generic-pae i686
我的测试结果
escape延后处理是需要性能代价的。但是这样可以减少业务复杂性。
场景1:No escape
渲染10万次,最快的是doU, doT, nTenjin(基于tenjin的优化版本), jst_speed;最慢的是tenjin和jade doT已经达到百万次级别了,好神速啊!
!!!新增 [@shaunlee](/user/shaunlee) 的中国第一速度模板引擎: jst, [@shaunlee](/user/shaunlee)看见第一次测试结果后非常不满意,经过优化后,速度已经达到第一梯队了!
No escape, render 100000 times:
doT running…
use: 0.066 sec, rps: 1515151.5151515151
doU running…
use: 0.064 sec, rps: 1562500
nTenjin running…
use: 0.069 sec, rps: 1449275.3623188403
jqtpl running…
use: 2.059 sec, rps: 48567.26566294317
ejs running…
use: 1.514 sec, rps: 66050.19815059446
haml running…
use: 9.532 sec, rps: 10490.97775912715
jade running…
use: 10.592 sec, rps: 9441.087613293052
jst running…
use: 2.089 sec, rps: 47869.79415988511
jst_speed running…
use: 0.065 sec, rps: 1538461.5384615385
场景2:All escape
性能马上变成浮云了。 速度高低排名: jst(果然是第一速度), nTenjin, jqtpl, ejs, doT, doU, haml, jade 对比场景1,可看到doT和doU的escape性能非常差。
All escape, render 100000 times:
doT running…
use: 3.921 sec, rps: 25503.698036215254
doU running…
use: 3.926 sec, rps: 25471.217524197655
nTenjin running…
use: 1.306 sec, rps: 76569.67840735069
jqtpl running…
use: 3.012 sec, rps: 33200.531208499335
ejs running…
use: 3.539 sec, rps: 28256.569652444192
haml running…
use: 11.098 sec, rps: 9010.632546404757
jade running…
use: 12.676 sec, rps: 7888.9239507731145
jst running…
use: 4.004 sec, rps: 24975.02497502498
jst_speed running…
use: 1.167 sec, rps: 85689.8029134533
从tenjin到nTenjin的几点性能优化方法
原文: https://github.com/QLeelulu/nTenjin/blob/master/README.md
- jsTenjin是使用eval来解析的,而nTenjin是使用 new Function 来解析的(速度差别之一)。
- jsTenjin是使用Array.push来构造字符串的,而nTenjin是使用 String += str 来构造字符串的(速度差别之二)。
- nTenjin中变量必须由it来指定,例如#{param}要修改为#{it.param},其他和jsTenjin完全一致。
模板引擎优化步骤
- 使用new Function 解析;
- 直接字符从相加 str += s;
- 变量不切换上下文,直接it指定
- 高性能的escape
Nodejs JavaScript模板引擎性能对比及几点优化
运行性能测试
测试代码可以在以下仓库中找到:
$ git clone https://github.com/fengmk2/fengmk2.github.com.git
$ cd fengmk2.github.com/blog/2011/04/js-template-benchmarks
$ node benchmarks.js
我的测试环境
CPU: 4核 Intel(R) Core(TM) i3 CPU M 330 @ 2.13GHz
Memory: 4GB
OS: Ubuntu 10.10 2.6.35-28-generic-pae i686
测试结果
场景1:No Escape
在没有转义的情况下,doT
和 jst_speed
是最快的,达到了百万次级别的渲染速度。
No escape, render 100000 times:
doT running...
use: 0.066 sec, rps: 1515151.5151515151
--------------------------------------------
doU running...
use: 0.064 sec, rps: 1562500
--------------------------------------------
nTenjin running...
use: 0.069 sec, rps: 1449275.3623188403
--------------------------------------------
jqtpl running...
use: 2.059 sec, rps: 48567.26566294317
--------------------------------------------
ejs running...
use: 1.514 sec, rps: 66050.19815059446
--------------------------------------------
haml running...
use: 9.532 sec, rps: 10490.97775912715
--------------------------------------------
jade running...
use: 10.592 sec, rps: 9441.087613293052
--------------------------------------------
jst running...
use: 2.089 sec, rps: 47869.79415988511
--------------------------------------------
jst_speed running...
use: 0.065 sec, rps: 1538461.5384615385
--------------------------------------------
场景2:All Escape
在所有内容都需要转义的情况下,jst_speed
依然是最快的。
All escape, render 100000 times:
doT running...
use: 3.921 sec, rps: 25503.698036215254
--------------------------------------------
doU running...
use: 3.926 sec, rps: 25471.217524197655
--------------------------------------------
nTenjin running...
use: 1.306 sec, rps: 76569.67840735069
--------------------------------------------
jqtpl running...
use: 3.012 sec, rps: 33200.531208499335
--------------------------------------------
ejs running...
use: 3.539 sec, rps: 28256.569652444192
--------------------------------------------
haml running...
use: 11.098 sec, rps: 9010.632546404757
--------------------------------------------
jade running...
use: 12.676 sec, rps: 7888.9239507731145
--------------------------------------------
jst running...
use: 4.004 sec, rps: 24975.02497502498
--------------------------------------------
jst_speed running...
use: 1.167 sec, rps: 85689.8029134533
--------------------------------------------
从tenjin到nTenjin的几点性能优化方法
-
使用
new Function
解析:jsTenjin
使用eval
,而nTenjin
使用new Function
,后者更快。const func = new Function('return "Hello, World!";'); console.log(func()); // Hello, World!
-
直接字符拼接:
jsTenjin
使用Array.push
构造字符串,而nTenjin
使用String += str
。let str = ''; for (let i = 0; i < 10000; i++) { str += 'Hello, World!'; } console.log(str); // Hello, World!... (repeated)
-
变量不切换上下文:在
nTenjin
中,变量必须通过it
指定。// jsTenjin #{param} // nTenjin #{it.param}
模板引擎优化步骤
- 使用
new Function
解析。 - 直接字符拼接
str += s
。 - 变量不切换上下文,直接使用
it
指定。 - 高性能的转义函数。
通过这些优化,可以显著提高模板引擎的渲染性能。
呵呵, 最近又在讨论tpl了, 重新顶一下这个, 任何模板引擎, 最终性能都能达到一样的水平…
@。@这个都还好吧。最终都要生成缓存页的。只要不是慢得离谱,我都能接受。 用哪个,一个看自己,一个看周围人的习惯。
Node.js JavaScript 模板引擎性能对比及几点优化
在Node.js项目中选择合适的模板引擎对于提升应用性能至关重要。以下是一些常见的Node.js模板引擎及其性能对比,以及一些优化建议。
性能测试结果
我们对几个流行的模板引擎进行了性能测试,包括doT、doU、nTenjin、jqtpl、ejs、haml、jade和jst。测试环境如下:
CPU: 4核 Intel(R) Core(TM) i3 CPU M 330 @ 2.13GHz
Memory: 4GB
OS: Ubuntu 10.10 2.6.35-28-generic-pae i686
场景1:No Escape
在未转义的情况下,doT、doU、nTenjin和jst_speed表现最佳:
doT running...
use: 0.066 sec, rps: 1515151.5151515151
--------------------------------------------
doU running...
use: 0.064 sec, rps: 1562500
--------------------------------------------
nTenjin running...
use: 0.069 sec, rps: 1449275.3623188403
--------------------------------------------
jst_speed running...
use: 0.065 sec, rps: 1538461.5384615385
场景2:All Escape
在所有转义的情况下,jst、nTenjin、jqtpl、ejs、doT、doU、haml和jade的表现如下:
jst running...
use: 4.004 sec, rps: 24975.02497502498
--------------------------------------------
nTenjin running...
use: 1.306 sec, rps: 76569.67840735069
--------------------------------------------
jst_speed running...
use: 1.167 sec, rps: 85689.8029134533
优化建议
-
使用
new Function
解析:- 使用
new Function
可以显著提高模板解析速度,因为它减少了编译时间。
- 使用
-
直接字符拼接:
- 使用字符串拼接
str += s;
比数组拼接Array.push
更高效。
- 使用字符串拼接
-
变量不切换上下文:
- 尽量使用统一的变量上下文,如
it
,避免频繁的上下文切换。
- 尽量使用统一的变量上下文,如
-
高性能的转义:
- 尽量将转义操作延迟处理,以减少性能开销。
示例代码
以下是一个简单的doT模板示例,展示了如何使用 new Function
和直接字符串拼接来优化模板引擎:
// 安装doT模板引擎
// npm install dot
const doT = require('dot');
// 编译模板
const template = doT.template('<div>{{= it.name }}</div>');
// 执行模板渲染
const result = template({ name: 'John Doe' });
console.log(result); // 输出: <div>John Doe</div>
// 高性能模板示例
function createTemplateFunction(templateStr) {
return new Function('it', `return \`${templateStr}\`;`);
}
const optimizedTemplate = createTemplateFunction('<div>{{= it.name }}</div>');
const optimizedResult = optimizedTemplate({ name: 'John Doe' });
console.log(optimizedResult); // 输出: <div>John Doe</div>
通过上述方法,可以显著提升Node.js模板引擎的性能。希望这些建议对你有所帮助。