forked from mauricemach/coffeekup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.coffee
198 lines (187 loc) · 5 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
coffeekup = require './lib/coffeekup'
jade = require 'jade'
ejs = require 'ejs'
eco = require 'eco'
haml = require 'haml'
puts = console.log
data =
title: 'test'
inspired: no
users: [
{email: '[email protected]', name: 'house'}
{email: '[email protected]', name: 'cuddy'}
{email: '[email protected]', name: 'wilson'}
]
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
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
'''
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)
benchmark = (title, code) ->
start = new Date
for i in [1..5000]
code()
puts "#{title}: #{new Date - start} ms"
@run = ->
benchmark 'CoffeeKup (precompiled)', -> coffeekup_compiled_template context: data
benchmark 'CoffeeKup (code)', -> coffeekup.render coffeekup_template, context: data
benchmark 'CoffeeKup (code, cache off)', -> coffeekup.render coffeekup_template, context: data, cache: off
benchmark 'CoffeeKup (string)', -> coffeekup.render coffeekup_string_template, context: data
benchmark 'CoffeeKup (string, cache off)', -> coffeekup.render coffeekup_string_template, context: data, cache: off
benchmark 'Jade (cache off)', -> jade.render jade_template, locals: data
benchmark 'Jade (cache on)', -> jade.render jade_template, locals: data, cache: on, filename: 'test'
benchmark 'ejs (cache off)', -> ejs.render ejs_template, locals: data
benchmark 'ejs (cache on)', -> ejs.render ejs_template, locals: data, cache: on, filename: 'test'
benchmark 'haml-js', -> haml.render haml_template, locals: data
benchmark 'haml-js (precompiled)', -> haml_template_compiled(data)
benchmark 'Eco', -> eco.render eco_template, data