Coming up to speed on Ember (very, very cool), and something about templates is bothering me. In an .hbs file, I have this HTML at the end of a minimal template that has some directives like {{#link-to}}
and such above it.
<div>
<div>
This is cool stuff here
</div>
</div>
When I use ember b -prod
to do a production build, the HTML above ends up being output as a function like this (I’ve pretty-printed it) inside an object initializer:
buildFragment: function() {
var t = e.createDocumentFragment(),
n = e.createComment("");
e.appendChild(t, n);
var n = e.createTextNode("\n");
e.appendChild(t, n);
var n = e.createComment("");
e.appendChild(t, n);
var n = e.createTextNode("\n\n");
e.appendChild(t, n);
var n = e.createElement("div"),
r = e.createTextNode("\n ");
e.appendChild(n, r);
var r = e.createElement("div"),
a = e.createTextNode("\n This is cool stuff here\n ");
e.appendChild(r, a), e.appendChild(n, r);
var r = e.createTextNode("\n");
e.appendChild(n, r), e.appendChild(t, n);
var n = e.createTextNode("\n");
return e.appendChild(t, n), t
}
A couple of things concern me there.
-
A long list of DOM API calls is necessarily slower than having the browser’s built-in parser handle parsing the HTML. Usually it won’t matter, of course, but if I extrapolate the above to the kinds of significant structures that we frequently have to build, I start getting concerned.
-
The code is much larger than the original HTML, concerning me (again, perhaps prematurely) in terms of load times and size-on-the-wire.
-
What’s with the
createComment
calls? There are no comments in the HTML. And all the repeatedvar
declarations for vars already declared?
If there’s a lot of intermix between directives and static content, there might be more benefit, but it seems odd that large sections of static HTML are handled so…verbosely.
My goal here is to understand how it works (did I do something wrong?), and to ask: Have the things above been issues for people in the real world? Are there reasonable alternatives (perhaps client-side compilation of templates into more efficient runtime structures) that are well-integrated into the excellent Ember structure?
Thanks,
– T.J.