-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.js
186 lines (147 loc) · 3.89 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
;(async () => {
const n = 1e5
const fs = require('fs')
const wait = () =>
new Promise(resolve => {
setTimeout(resolve, 500)
})
const tpl = fs.readFileSync('bench.html', 'utf8')
const tplZup = fs.readFileSync('benchZup.html', 'utf8')
const tplEjs = tplZup.replace(/\[\[/g, '<%').replace(/\]\]/g, '%>')
const tplDot = tplZup
const tplEdge = fs.readFileSync('benchEdge.html', 'utf8')
const compile = require('../compile.js')
const zupcompile = require('zup')
const ejscompile = require('ejs').compile
const dotcompile = require('dot').template
const edge = require('edge.js')
const vars = {
heading: 'This title will be truncated',
content: `
My life story, and I'm not kidding this time...
`,
alert: '<b>HI MOM!</b>'
}
const opts = null
const zupOpts = {}
const ejsOpts = {localsName: 'z'}
const dotOpts = {
evaluate: /\[\[([\s\S]+?)\]\]/g,
interpolate: /\[\[-([\s\S]+?)\]\]/g,
encode: /\[\[=([\s\S]+?)\]\]/g,
varname: 'z',
strip: false
}
const edgeOpts = null
const fn = compile(tpl)
const zupfn = zupcompile(tplZup, zupOpts)
const ejsfn = ejscompile(tplEjs, ejsOpts)
const dotfn = dotcompile(tplDot, dotOpts)
const edgefn = data => edge.renderString(tplEdge, data)
console.log('--------- console.time Compile ---------')
edge.configure({
cache: false
})
console.time('literal compile')
for (var i = 0; i < n; ++i) compile(tpl)
console.timeEnd('literal compile')
await wait()
console.time('zup compile')
for (var i = 0; i < n; ++i) zupcompile(tplZup, zupOpts)
console.timeEnd('zup compile')
await wait()
console.time('ejs compile')
for (var i = 0; i < n; ++i) ejscompile(tplEjs, ejsOpts)
console.timeEnd('ejs compile')
await wait()
console.time('doT compile')
for (var i = 0; i < n; ++i) dotcompile(tplDot, dotOpts)
console.timeEnd('doT compile')
await wait()
console.time('edge compile')
for (var i = 0; i < n; ++i) edge.compileString(tplEdge)
console.timeEnd('edge compile')
await wait()
console.log('--------- console.time Render ---------')
edge.configure({
cache: true
})
console.time('literal render')
for (var i = 0; i < n; ++i) fn(vars)
console.timeEnd('literal render')
await wait()
console.time('zup render')
for (var i = 0; i < n; ++i) zupfn(vars)
console.timeEnd('zup render')
await wait()
console.time('ejs render')
for (var i = 0; i < n; ++i) ejsfn(vars)
console.timeEnd('ejs render')
await wait()
console.time('doT render')
for (var i = 0; i < n; ++i) dotfn(vars)
console.timeEnd('doT render')
await wait()
console.time('edge render')
for (var i = 0; i < n; ++i) edgefn(vars)
console.timeEnd('edge render')
await wait()
console.log('--------- Benchmark Compile ---------')
edge.configure({
cache: false
})
const Benchmark = require('benchmark')
let suite = new Benchmark.Suite()
suite
.add('Literal compile', function() {
compile(tpl)
})
.add('Zup compile', function() {
zupcompile(tplZup, zupOpts)
})
.add('Ejs compile', function() {
ejscompile(tplEjs, ejsOpts)
})
.add('Dot compile', function() {
dotcompile(tplDot, dotOpts)
})
.add('Edge compile', function() {
edge.compileString(tplEdge)
})
.on('cycle', function(event) {
console.log(String(event.target))
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run()
await wait()
console.log('--------- Benchmark Render ---------')
edge.configure({
cache: true
})
suite = new Benchmark.Suite()
suite
.add('Literal render', function() {
fn(vars)
})
.add('Zup render', function() {
zupfn(vars)
})
.add('Ejs render', function() {
ejsfn(vars)
})
.add('Dot render', function() {
dotfn(vars)
})
.add('Edge render', function() {
edgefn(vars)
})
.on('cycle', function(event) {
console.log(String(event.target))
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run()
})()