2007年 12月 26日

ejs.js はやくなった

コンパイル時間はともかく、実行時間が replace による簡単な置換の2倍程度まではやくなった。

COUNT = 500;

var t = "aaaa<%=s.foo%>bbbbb<%=s.bar%>ccc";
var e = EJS(t);
var f = EJS(t, {useWith:true});
var m = {foo:"test", bar:"foobar"};
var b = [
	function compile () {
		EJS(t);
	},
	function processing () {
		e.run(m);
	},
	function processing_with_with () {
		f.run(m);
	},
	function replace () {
		t.replace(/<%=s\.(\w+)%>/, function (_,a) {
			return m[a];
		});
	}
];


for (var i = 0; i < b.length; i++) {
	var fun = b[i];
	print(fun.name);
	var res = 0;
	for (var j = 0; j < COUNT; j++) {
		var now = (new Date).getTime();
		fun();
		res += (new Date).getTime() - now;
	}
	print(res + "ms / " + (res/COUNT) + "ms");
};
Spidermonkey
compile
283ms / 0.566ms
processing
25ms / 0.05ms
processing_with_with
37ms / 0.074ms
replace
12ms / 0.024ms

Rhino
compile
5094ms / 10.188ms
processing
500ms / 1ms
processing_with_with
670ms / 1.34ms
replace
279ms / 0.558ms

ejs.js の高速化より Spidermonkey と Rhino の速度差のほうに驚いた

replace の正規表現に g がついてないから、実際は replace はもうすこし遅いですね