forked from barisusakli/template-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
197 lines (168 loc) · 5.11 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
var fs = require('fs');
var path = require('path');
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 pug = require('./pug/pug.js');
var pugWithoutWith = require('./pug-without-with/pug.js');
var eco = require('./eco/eco.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 benchpress = require('./benchpress/benchpress.js');
var twig = require('./twig/twig.js');
function noop() {}
function test(name, sample, cb) {
var callback = cb;
var i = 0;
var start;
function done(error /* , html */) {
if (error) {
callback(error);
callback = noop;
return;
}
i += 1;
if (i === count) {
var diff = process.hrtime(start);
callback(null, name, diff);
}
}
sample.prepare(data, function () {
start = process.hrtime();
for (var j = 0; j < count; j += 1) {
sample.step(done);
}
});
}
function testUnescaped(name, sample, cb) {
var callback = cb;
var i = 0;
var start;
function done(error /* , html */) {
if (error) {
callback(error);
callback = noop;
return;
}
i += 1;
if (i === count) {
var diff = process.hrtime(start);
callback(null, name, diff);
}
}
sample.prepareUnescaped(data, function () {
start = process.hrtime();
for (var j = 0; j < count; j += 1) {
sample.step(done);
}
});
}
var samples = [
{ name: 'Pug', sample: pug },
{ name: 'Pug without `with`', sample: pugWithoutWith },
{ name: 'CoffeeKup', sample: coffeekup },
{ name: 'Handlebars.js', sample: handlebars },
{ name: 'EJS', sample: ejs },
{ name: 'Eco', sample: eco },
{ name: 'Underscore', sample: underscore },
{ 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 },
{ name: 'Benchpress.js', sample: benchpress },
{ name: 'Twig.js', sample: twig },
];
function pad(val, num, pre) {
var value = typeof val === 'string' ? val : '' + val;
while (value.length < num) {
value = (pre ? ' ' : '') + value + (pre ? '' : ' ');
}
return value;
}
function parseTime(hrtime) {
return (hrtime[0] * 1000) + (hrtime[1] / 1e6);
}
var reportOutput = '';
var detailedOutput = '';
function report(str) {
reportOutput += '\n' + str;
console.log(str);
}
function detail(str) {
detailedOutput += '\n' + str;
console.log(str);
}
function writeReport() {
report('\n\n### [See stats by engine](detail.' + process.platform + '.txt)');
fs.writeFileSync(path.join(__dirname, 'reports', 'report.' + process.platform + '.md'), reportOutput);
fs.writeFileSync(path.join(__dirname, 'reports', 'detail.' + process.platform + '.txt'), detailedOutput);
}
var results = [];
function runTests() {
if (samples.length) {
var sample = samples.pop();
test(sample.name, sample.sample, function (err, name, result) {
if (err) {
throw err;
}
testUnescaped(sample.name, sample.sample, function (errUn, nameUn, resultUn) {
if (err) {
throw err;
}
var escaped = parseTime(result);
var unescaped = parseTime(resultUn);
detail(sample.name);
detail(' Escaped : ' + escaped.toFixed() + 'ms');
detail(' Unescaped : ' + unescaped.toFixed() + 'ms');
detail(' Total : ' + (escaped + unescaped).toFixed() + 'ms');
detail('');
results.push({
name: sample.name,
escaped: escaped,
unescaped: unescaped,
total: escaped + unescaped,
});
runTests();
});
});
} else {
report('Performance report for ' + count + ' templates (' + process.platform + ')');
report('===========================');
['total', 'escaped', 'unescaped'].forEach(function (prop) {
results.sort(function (a, b) {
var x = a[prop];
var y = b[prop];
return x - y;
});
report('\n' + prop + ' duration\n--------------');
report('| | | |');
report('|----------------------|---------|-----------|');
var fastest = results[0][prop];
results.forEach(function (result, i) {
var percentage = Math.round((100 / fastest * result[prop]) - 100);
report(
'| ' + pad(result.name, 20) +
' | ' + pad(result[prop].toFixed(), 5, true) + 'ms' +
' | ' + (i === 0 ? 'fastest' : percentage + '% slower') + ' |'
);
});
});
writeReport();
}
}
detail('Rendering ' + count + ' templates:\n');
runTests();