forked from baryshev/template-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.js
121 lines (114 loc) · 3.46 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var data = require('./data');
var count = 100000;
var ect = require('./ect/ect.js');
var ejs = require('./ejs/ejs.js');
var ejsWithoutWith = require('./ejs-without-with/ejs.js');
var jade = require('./jade/jade.js');
var jadeWithoutWith = require('./jade-without-with/jade.js');
var eco = require('./eco/eco.js');
var swig = require('./swig/swig.js');
var hogan = require('./hogan/hogan.js');
var dust = require('./dust/dust.js');
var fest = require('./fest/fest.js');
var dot = require('./dot/dot.js');
var templatesjs = require('./tjs/t.js');
var handlebars = require('./handlebars/handlebars.js');
var coffeekup = require('./coffeekup/coffeekup.js');
var underscore = require('./underscore/underscore.js');
var gaikan = require('./gaikan/gaikan.js');
var test = function(name, sample, cb) {
var i = 0;
var start;
var done = function(error, html) {
i++;
if (i === count) {
var now = Date.now();
cb(null, name, now - start);
}
}
sample.prepare(data, function() {
start = Date.now();
for (var j = 0; j < count; j++) {
sample.step(done);
}
});
};
var testUnescaped = function(name, sample, cb) {
var i = 0;
var start;
var done = function(error, html) {
i++;
if (i === count) {
var now = Date.now();
cb(null, name, now - start);
}
}
sample.prepareUnescaped(data, function() {
start = Date.now();
for (var j = 0; j < count; j++) {
sample.step(done);
}
});
};
var samples = [
{ name : 'Jade', sample : jade },
{ name : 'CoffeeKup', sample : coffeekup },
{ name : 'Jade without `with`', sample : jadeWithoutWith },
{ name : 'Handlebars.js', sample : handlebars },
{ name : 'EJS', sample : ejs },
{ name : 'Eco', sample : eco },
{ name : 'Underscore', sample : underscore },
{ name : 'Swig', sample : swig },
{ name : 'doT', sample : dot },
{ name : 'EJS without `with`', sample : ejsWithoutWith },
{ name : 'Fest', sample : fest },
{ name : 'Hogan.js', sample : hogan },
{ name : 'Dust', sample : dust },
{ name : 'Gaikan', sample: gaikan },
{ name : 'ECT', sample : ect },
{ name : 'templates.js', sample: templatesjs },
];
var results = [];
var pad = function (val, num, pre) {
val = typeof val === 'string' ? val : '' + val;
while (val.length < num) val = (pre ? ' ' : '') + val + (pre ? '' : ' ');
return val;
};
var runTests = function () {
if (samples.length) {
var sample = samples.pop();
test(sample.name, sample.sample, function (err, name, result) {
testUnescaped(sample.name, sample.sample, function (err, name, resultUnescaped) {
console.log(name);
console.log(' Escaped : ' + result + 'ms');
console.log(' Unescaped : ' + resultUnescaped + 'ms');
console.log(' Total : ' + (result + resultUnescaped) + 'ms');
console.log('');
results.push({
name: name,
escaped: result,
unescaped: resultUnescaped,
total: result + resultUnescaped
});
runTests();
});
});
} else {
console.log('Performance report for ' + count + ' templates (' + process.platform + '):\n');
results.sort(function (a, b) {
var x = a.total;
var y = b.total;
return x < y ? -1 : (x > y ? 1 : 0);
});
var fastest = results[0].total;
for (var i = 0; i < results.length; i += 1) {
var result = results[i];
var percentage = Math.round((100 / fastest * result.total) - 100);
console.log(pad(result.name, 20) +
' (' + pad(result.total, 5, true) + 'ms)' +
(i == 0 ? ' - fastest' : ' - ' + percentage + '% slower'));
}
}
};
console.log('Rendering ' + count + ' templates:\n');
runTests();