Skip to content

Instantly share code, notes, and snippets.

@mgutz
Created January 30, 2011 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgutz/802424 to your computer and use it in GitHub Desktop.
Save mgutz/802424 to your computer and use it in GitHub Desktop.
jquery-tmpl slow compared to string substitution
head.ready ->
# create 10,000 messages
s = ''
messages = []
for i in [0...10000]
messages.push from: 'me', text: "blah blah"
#//// jquery-tmpl
$.template 'chatTemplate',
"""
{{each messages}}
<li>
<div class='from'>
${from}
</div>
<div class='text'>
${text}
</div>
</li>
{{/each}}
"""
out = $.tmpl('chatTemplate', messages: messages)
$(out).appendTo 'ul'
#//// Same Using String with Proper HTML escaping
h = (s) ->
s.replace(/&/g,'&amp;').
replace(/>/g,'&gt;').
replace(/</g,'&lt;').
replace(/"/g,'&quot;')
for msg in messages
s +=
"""
<li>
<div class='from'>
#{h msg.from}
</div>
<div class='text'>
#{h msg.text}
</div>
</li>
"""
$(s).appendTo 'ul'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment