Skip to content

Instantly share code, notes, and snippets.

@jamalex
Created April 15, 2015 04:14
Show Gist options
  • Save jamalex/a0ce3b213b9362c8ba46 to your computer and use it in GitHub Desktop.
Save jamalex/a0ce3b213b9362c8ba46 to your computer and use it in GitHub Desktop.
Handlebars.js vs Django templates benchmark
import time
from django.template import Template, Context
from django.conf import settings
settings.configure()
source = """<p>Hello, my name is {{name}}. I am from {{hometown}}. I have {{kids|length}} kids:</p>
<ul>{% for kid in kids %}<li>{{kid.name}} is {{kid.age}}</li>{% endfor %}</ul>"""
template = Template(source)
data = {"name": "Alan", "hometown": "Somewhere, TX",
"kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]}
context = Context(data)
t = time.time()
for i in range(100000):
template.render(context)
print time.time() - t
// Uncomment the following line if running in Node.js
// var Handlebars = require("handlebars");
var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
"{{kids.length}} kids:</p>" +
"<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
var template = Handlebars.compile(source);
var data = { "name": "Alan", "hometown": "Somewhere, TX",
"kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
console.time("render");
for (var i = 0; i < 100000; i++) {
var result = template(data);
}
console.timeEnd("render");
$ python dj.py
22.1622719765 (seconds)
$ node hb.js
render: 401ms
(note that this was ~600ms when run in Chrome)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment