UNPKG

5.54 kBtext/coffeescriptView Raw
1#!/usr/bin/env coffee
2
3coffeecup = require './src/coffeecup'
4jade = require 'jade'
5ejs = require 'ejs'
6eco = require 'eco'
7haml = require 'haml'
8log = console.log
9
10data =
11 title: 'test'
12 inspired: no
13 users: [
14 {email: 'house@gmail.com', name: 'house'}
15 {email: 'cuddy@gmail.com', name: 'cuddy'}
16 {email: 'wilson@gmail.com', name: 'wilson'}
17 ]
18
19coffeecup_template = ->
20 doctype 5
21 html lang: 'en', ->
22 head ->
23 meta charset: 'utf-8'
24 title @title
25 style '''
26 body {font-family: "sans-serif"}
27 section, header {display: block}
28 '''
29 body ->
30 section ->
31 header ->
32 h1 @title
33 if @inspired
34 p 'Create a witty example'
35 else
36 p 'Go meta'
37 ul ->
38 for user in @users
39 li user.name
40 li -> a href: "mailto:#{user.email}", -> user.email
41
42coffeecup_string_template = """
43 doctype 5
44 html lang: 'en', ->
45 head ->
46 meta charset: 'utf-8'
47 title @title
48 style '''
49 body {font-family: "sans-serif"}
50 section, header {display: block}
51 '''
52 body ->
53 section ->
54 header ->
55 h1 @title
56 if @inspired
57 p 'Create a witty example'
58 else
59 p 'Go meta'
60 ul ->
61 for user in @users
62 li user.name
63 li -> a href: "mailto:\#{user.email}", -> user.email
64"""
65
66coffeecup_compiled_template = coffeecup.compile coffeecup_template
67
68coffeecup_optimized_template = coffeecup.compile coffeecup_template, optimize: yes
69
70jade_template = '''
71 !!! 5
72 html(lang="en")
73 head
74 meta(charset="utf-8")
75 title= title
76 style
77 | body {font-family: "sans-serif"}
78 | section, header {display: block}
79 body
80 section
81 header
82 h1= title
83 - if (inspired)
84 p Create a witty example
85 - else
86 p Go meta
87 ul
88 - each user in users
89 li= user.name
90 li
91 a(href="mailto:"+user.email)= user.email
92'''
93
94jade_compiled_template = jade.compile jade_template
95
96ejs_template = '''
97 <!DOCTYPE html>
98 <html lang="en">
99 <head>
100 <meta charset="utf-8">
101 <title><%= title %></title>
102 <style>
103 body {font-family: "sans-serif"}
104 section, header {display: block}
105 </style>
106 </head>
107 <body>
108 <section>
109 <header>
110 <h1><%= title %></h1>
111 </header>
112 <% if (inspired) { %>
113 <p>Create a witty example</p>
114 <% } else { %>
115 <p>Go meta</p>
116 <% } %>
117 <ul>
118 <% for (user in users) { %>
119 <li><%= user.name %></li>
120 <li><a href="mailto:<%= user.email %>"><%= user.email %></a></li>
121 <% } %>
122 </ul>
123 </section>
124 </body>
125 </html>
126'''
127
128eco_template = '''
129 <!DOCTYPE html>
130 <html lang="en">
131 <head>
132 <meta charset="utf-8">
133 <title><%= @title %></title>
134 <style>
135 body {font-family: "sans-serif"}
136 section, header {display: block}
137 </style>
138 </head>
139 <body>
140 <section>
141 <header>
142 <h1><%= @title %></h1>
143 </header>
144 <% if @inspired: %>
145 <p>Create a witty example</p>
146 <% else: %>
147 <p>Go meta</p>
148 <% end %>
149 <ul>
150 <% for user in @users: %>
151 <li><%= user.name %></li>
152 <li><a href="mailto:<%= user.email %>"><%= user.email %></a></li>
153 <% end %>
154 </ul>
155 </section>
156 </body>
157 </html>
158'''
159
160eco_compiled_template = eco.compile eco_template
161
162haml_template = '''
163 !!! 5
164 %html{lang: "en"}
165 %head
166 %meta{charset: "utf-8"}
167 %title= title
168 :css
169 body {font-family: "sans-serif"}
170 section, header {display: block}
171 %body
172 %section
173 %header
174 %h1= title
175 :if inspired
176 %p Create a witty example
177 :if !inspired
178 %p Go meta
179 %ul
180 :each user in users
181 %li= user.name
182 %li
183 %a{href: "mailto:#{user.email}"}= user.email
184'''
185
186haml_template_compiled = haml(haml_template)
187
188benchmark = (title, code) ->
189 start = new Date
190 for i in [1..5000]
191 code()
192 log "#{title}: #{new Date - start} ms"
193
194
195benchmark 'coffeecup (precompiled)', -> coffeecup_compiled_template data
196benchmark 'coffeecup (precompiled, optimized)', -> coffeecup_optimized_template data
197benchmark 'Jade (precompiled)', -> jade_compiled_template data
198benchmark 'haml-js (precompiled)', -> haml_template_compiled data
199benchmark 'Eco (precompiled)', -> eco_compiled_template data
200
201console.log '\n'
202
203benchmark 'coffeecup (function, cache on)', -> coffeecup.render coffeecup_template, data, cache: on
204benchmark 'coffeecup (string, cache on)', -> coffeecup.render coffeecup_string_template, data, cache: on
205#benchmark 'Jade (cache on)', -> jade.render jade_template, locals: data, cache: on, filename: 'test'
206benchmark 'ejs (cache on)', -> ejs.render ejs_template, locals: data, cache: on, filename: 'test'
207benchmark 'Eco', -> eco.render eco_template, data
208
209console.log '\n'
210
211benchmark 'coffeecup (function, cache off)', -> coffeecup.render coffeecup_template, data, cache: off
212benchmark 'coffeecup (string, cache off)', -> coffeecup.render coffeecup_string_template, data, cache: off
213#benchmark 'Jade (cache off)', -> jade.render jade_template, locals: data
214benchmark 'haml-js', -> haml.render haml_template, locals: data
215benchmark 'ejs (cache off)', -> ejs.render ejs_template, locals: data
216