forked from gradus/coffeecup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.coffee
214 lines (195 loc) · 5.41 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
#!/usr/bin/env coffee
coffeecup = require './src/coffeecup'
jade = require 'jade'
ejs = require 'ejs'
eco = require 'eco'
haml = require 'haml'
log = console.log
data =
title: 'test'
inspired: no
users: [
{email: 'house@gmail.com', name: 'house'}
{email: 'cuddy@gmail.com', name: 'cuddy'}
{email: 'wilson@gmail.com', name: 'wilson'}
]
coffeecup_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
coffeecup_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
"""
coffeecup_compiled_template = coffeecup.compile coffeecup_template
coffeecup_optimized_template = coffeecup.compile coffeecup_template, optimize: yes
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
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>
'''
eco_compiled_template = eco.compile eco_template
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)
benchmark = (title, code) ->
start = new Date
for i in [1..5000]
code()
log "#{title}: #{new Date - start} ms"
benchmark 'coffeecup (precompiled)', -> coffeecup_compiled_template data
benchmark 'coffeecup (precompiled, optimized)', -> coffeecup_optimized_template data
benchmark 'Jade (precompiled)', -> jade_compiled_template data
benchmark 'haml-js (precompiled)', -> haml_template_compiled data
benchmark 'Eco (precompiled)', -> eco_compiled_template data
console.log '\n'
benchmark 'coffeecup (function, cache on)', -> coffeecup.render coffeecup_template, data, cache: on
benchmark 'coffeecup (string, cache on)', -> coffeecup.render coffeecup_string_template, data, cache: on
#benchmark 'Jade (cache on)', -> jade.render jade_template, locals: data, cache: on, filename: 'test'
benchmark 'ejs (cache on)', -> ejs.render ejs_template, locals: data, cache: on, filename: 'test'
benchmark 'Eco', -> eco.render eco_template, data
console.log '\n'
benchmark 'coffeecup (function, cache off)', -> coffeecup.render coffeecup_template, data, cache: off
benchmark 'coffeecup (string, cache off)', -> coffeecup.render coffeecup_string_template, data, cache: off
#benchmark 'Jade (cache off)', -> jade.render jade_template, locals: data
benchmark 'haml-js', -> haml.render haml_template, locals: data
benchmark 'ejs (cache off)', -> ejs.render ejs_template, locals: data