forked from baryshev/template-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.js
94 lines (87 loc) · 2.57 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
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 handlebars = require('./handlebars/handlebars.js');
var coffeekup = require('./coffeekup/coffeekup.js');
var underscore = require('./underscore/underscore.js');
var gaikan = require('./gaikan/gaikan.js');
var slm = require('./slm/slm');
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 : 'Eco', sample : eco },
{ name : 'EJS', sample : ejs },
{ name : 'Underscore', sample : underscore },
{ name : 'Swig', sample : swig },
{ name : 'doT', sample : dot },
{ name : 'EJS without `with`', sample : ejsWithoutWith },
{ name : 'Fest', sample : fest },
{ name : 'Gaikan', sample: gaikan },
{ name : 'Hogan.js', sample : hogan },
{ name : 'Dust', sample : dust },
{ name : 'ECT', sample : ect },
{ name : 'Slm', sample : slm },
];
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('');
runTests();
});
});
}
};
console.log('Rendering ' + count + ' templates:\n');
runTests();