String Templating Considered Harmful
flippinawesome.org
flippinawesome.org
> Because it only touches the parts of the DOM it needs to, you can write your templates in the way that makes the most sense for your app, without having to worry about the performance headaches of constantly trashing the DOM or the code headache of wiring up a plethora of view objects.
Huh, the solution he described is basically the technique I use in Mithril[1], except that Mithril templates don't need the extra HTML parsing step because they pretty much are the AST.
My bare-bones approach received some criticism here last week for not being "pretty" (for the "HTML is pretty" definition of "pretty"), but it allows better debuggability[2], and opens the doors for some interesting FP techniques, among other things.
Templating seems to be a hot button these days, but it's unfortunate that it's such a complex topic. Discussions about performance like this article are great, but it's hard for non-framework devs to keep all the other facets of template design in their heads when trying to evaluate the bazillion choices in the market.
An important sub-title which needs to be highlighted, as these arguments and micro-benchmarks do not apply to most people who use string templating server side.
[0] https://wiki.python.org/moin/Templating#Template_engines_imp...
* less expressive
I somewhat disagree, and would also say that the only reason for having the extra "expressiveness" is to do things you probably shouldn't do in your template.
* you may need to avoid a flash of unbound content
But you may not!
* server-side rendering is hard or impossible without hacks
But I'd say that rendering string templates server-side is simply bad if you do not comprehend the HTML, so the "hacks" are worth it, and not really a big problem with -- say -- node.
* data-binding cruft
Simply disagree here -- make your data-binding "cruft" legal and it helps see what's going on in debugging.
So I'll stick with our "DOM-templating" approach. Good read though and thought-provoking.
A fork of the fork from the article using async measuring: http://jsfiddle.net/yyx990803/VrLjY/
It's easy to notice that the majority of the time is actually spent on the repaint and the difference between implementation becomes much less significant. The only takeaway from this benchmark is that innerHTML/DOM thrashing your entire view is slow, and as long as data binding is implemented in a sane manner, it is almost never the bottleneck.
The arguments against DOM templating are, as the author admitted, pretty opinionated. Personally for me I much prefer working with real HTML and find it more readable. However, I do agree the real advantage of a virtual/parallel DOM implementation is the ability to render isomorphically on both client and server, and it is something most DOM-based templating solutions haven’t been able to properly address yet (although not impossible).
The benchmark evaluates the overhead of data book keeping for one class of problems (updating a bunch of bindings all at once). Just because the paint time in this particular example dominates doesn't mean that the benchmark is inaccurate, as many applications using some sort of data binding-ish technique are indeed dominated by JS rather than paint time.
> and as long as data binding is implemented in a sane manner, it is almost never the bottleneck.
Maybe for small apps.
Specifically, digging into RActive it seems like it may be good for more straightforward data binding use cases. The template engine chooser was also new to me, and fun to run through: http://garann.github.io/template-chooser/
Clojure https://github.com/cgrand/enlive