views:

30

answers:

1

Hi there!

This question might be theoretical but i think some of the cases it makes sense. I am just wondering about which solution is the most efficient: loading HTML templates or build up them with DOM functions?

Both has pros/cons and there's a lot of other factors that can close off any of them: For example it's obvious that DOM-coded HTML can't be as easy to modify than the static one.

Fact: there's a zillion factors that can surely affect performance (disk IO, memory, CPU, bad code, etc.).

Question: is it a good practice to use DOM-coded HTML (fragments) as templates? Or in this case i'll be fine with string concatenation?

Thanks, fabrik

A: 

Templates are faster, in my test cases (of which results I am missing now) in comparison to creating HTML by DOM.

[EDIT]: Clarification to OP:

As I said, it was long ago (6-8 months I think) when I ran the test, I have lost the results. Well, since you need to load the data from disk ONLY ONCE, it does not create that much difference. In my tests, I had 100 variables per page. That would require DOM to insert elements 100 times or templating 100 times. The results showed a ratio of 1:1.35 (running time) in favor of templates, as far as I remember. Hope this helps.

shamittomar
My first bet was the disk io is much more expensive than some cpu-cycles to generate the same data. Can you show some results anytime? It would be interesting to see how the test cases built and how much was the difference.
fabrik
@Fabrik, answer updated.
shamittomar
@shamittomar: Thank you for your explanation. As i understand your template was a complex HTML page not a fragment (e.g. like a table row). My original curiosity was about small fragments especially: is it worth to save/load these fragment rather than generating directly?
fabrik
IMHO, DOM still needs loading of classes, creating objects and overheads. While simple template is nothing but a concatenation of strings and sting replaces. So, it should be faster.
shamittomar