-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark.coffee
290 lines (270 loc) · 6.86 KB
/
benchmark.coffee
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
coffeemugg = require './lib/coffeemugg'
log = console.log
try
coffeekup = require 'coffeecup'
catch e
log "coffeecup not installed"
try
jade = require 'jade'
catch e
log "jade not installed"
try
ejs = require 'ejs'
catch e
log "ejs not installed"
try
eco = require 'eco'
catch e
log "eco not installed"
try
haml = require 'haml'
catch e
log "haml not installed"
data =
title: 'test'
inspired: no
users: [
{email: 'house@gmail.com', name: 'house'}
{email: 'cuddy@gmail.com', name: 'cuddy'}
{email: 'wilson@gmail.com', name: 'wilson'}
]
coffeemugg_template = ->
@doctype 5
@html lang: 'en', ->
@head ->
@meta charset: 'utf-8'
@title data.title
@style '''
body {font-family: "sans-serif"}
section, header {display: block}
'''
@body ->
@section ->
@header ->
@h1 data.title
if data.inspired
@p 'Create a witty example'
else
@p 'Go meta'
@ul ->
for user in data.users
@li user.name
@li -> @a href: "mailto:#{user.email}", -> user.email
coffeemugg_template_args = (data) ->
@doctype 5
@html lang: 'en', ->
@head ->
@meta charset: 'utf-8'
@title data.title
@style '''
body {font-family: "sans-serif"}
section, header {display: block}
'''
@body ->
@section ->
@header ->
@h1 data.title
if data.inspired
@p 'Create a witty example'
else
@p 'Go meta'
@ul ->
for user in data.users
@li user.name
@li -> @a href: "mailto:#{user.email}", -> user.email
coffeekup_template = ->
doctype 5
html lang: 'en', ->
head ->
meta charset: 'utf-8'
title @title
style '''
body {font-family: "sans-serif"}
section, header {display: block}
'''
body ->
section ->
header ->
h1 @title
if @inspired
p 'Create a witty example'
else
p 'Go meta'
ul ->
for user in @users
li user.name
li -> a href: "mailto:#{user.email}", -> user.email
coffeekup_string_template = """
doctype 5
html lang: 'en', ->
head ->
meta charset: 'utf-8'
title @title
style '''
body {font-family: "sans-serif"}
section, header {display: block}
'''
body ->
section ->
header ->
h1 @title
if @inspired
p 'Create a witty example'
else
p 'Go meta'
ul ->
for user in @users
li user.name
li -> a href: "mailto:\#{user.email}", -> user.email
"""
coffeekup_compiled_template = coffeekup.compile coffeekup_template if coffeekup
jade_template = '''
!!! 5
html(lang="en")
head
meta(charset="utf-8")
title= title
style
| body {font-family: "sans-serif"}
| section, header {display: block}
body
section
header
h1= title
- if (inspired)
p Create a witty example
- else
p Go meta
ul
- each user in users
li= user.name
li
a(href="mailto:"+user.email)= user.email
'''
jade_compiled_template = jade.compile jade_template if jade
ejs_template = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><%= title %></title>
<style>
body {font-family: "sans-serif"}
section, header {display: block}
</style>
</head>
<body>
<section>
<header>
<h1><%= title %></h1>
</header>
<% if (inspired) { %>
<p>Create a witty example</p>
<% } else { %>
<p>Go meta</p>
<% } %>
<ul>
<% for (user in users) { %>
<li><%= user.name %></li>
<li><a href="mailto:<%= user.email %>"><%= user.email %></a></li>
<% } %>
</ul>
</section>
</body>
</html>
'''
eco_template = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><%= @title %></title>
<style>
body {font-family: "sans-serif"}
section, header {display: block}
</style>
</head>
<body>
<section>
<header>
<h1><%= @title %></h1>
</header>
<% if @inspired: %>
<p>Create a witty example</p>
<% else: %>
<p>Go meta</p>
<% end %>
<ul>
<% for user in @users: %>
<li><%= user.name %></li>
<li><a href="mailto:<%= user.email %>"><%= user.email %></a></li>
<% end %>
</ul>
</section>
</body>
</html>
'''
haml_template = '''
!!! 5
%html{lang: "en"}
%head
%meta{charset: "utf-8"}
%title= title
:css
body {font-family: "sans-serif"}
section, header {display: block}
%body
%section
%header
%h1= title
:if inspired
%p Create a witty example
:if !inspired
%p Go meta
%ul
:each user in users
%li= user.name
%li
%a{href: "mailto:#{user.email}"}= user.email
'''
haml_template_compiled = haml(haml_template) if haml
benchmark = (title, code) ->
start = new Date
for i in [1..15000]
code()
log "#{title}: #{new Date - start} ms"
@run = ->
benchmark 'CoffeeMugg (none)', -> coffeemugg.render coffeemugg_template
benchmark 'CoffeeMugg (args)', -> coffeemugg.render coffeemugg_template_args, null, data
benchmark 'CoffeeMugg (format) (none)', -> coffeemugg.render coffeemugg_template, format:on
context = coffeemugg.CMContext()
benchmark 'CoffeeMugg (reuse context)', ->
context.reset().render coffeemugg_template
console.log '\n'
if coffeekup
benchmark 'CoffeeKup (precompiled)', -> coffeekup_compiled_template data
if jade
benchmark 'Jade (precompiled)', -> jade_compiled_template data
if haml
benchmark 'haml-js (precompiled)', -> haml_template_compiled data
if eco
benchmark 'Eco', -> eco.render eco_template, data
console.log '\n'
if coffeekup
benchmark 'CoffeeKup (function, cache on)', -> coffeekup.render coffeekup_template, data, cache: on
benchmark 'CoffeeKup (string, cache on)', -> coffeekup.render coffeekup_string_template, data, cache: on
### broken
if jade
benchmark 'Jade (cache on)', -> jade.render jade_template, locals: data, cache: on, filename: 'test'
###
if ejs
benchmark 'ejs (cache on)', -> ejs.render ejs_template, locals: data, cache: on, filename: 'test'
console.log '\n'
### broken
if jade
benchmark 'Jade (cache off)', -> jade.render jade_template, locals: data
###
if haml
benchmark 'haml-js', -> haml.render haml_template, locals: data
if ejs
benchmark 'ejs (cache off)', -> ejs.render ejs_template, locals: data