Created
April 15, 2015 04:14
-
-
Save jamalex/a0ce3b213b9362c8ba46 to your computer and use it in GitHub Desktop.
Handlebars.js vs Django templates benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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