When generating dynamic content, which way is better to simply create the HTML and insert it into a .innerHTML or to create elements in the DOM directly?
I’m not concerned with complexity or other. Just processing time on the client.
Which is quicker?
If I had to guess in order of efficiency (low processing time) this would be:
- DOM Creation
- .innerHTML property insertion
- direct writing
This would be inversely related to the complexity of implmenting:
- direct writing
- .innerHTML property insertion
- DOM Creation
This is a validatin question? Can someone validate that this is the trade-offs for determining how to update the client (i.e. javascript) when considering complexity and speed?
Research:
I’m not concerned with security like they are here->
InnerHTML vs. DOM – Security
InnerHTML vs. DOM
This is not a duplicate as it covers only part of my question.
From my own personal tests they’re all fast enough for most needs. But if you’re doing something crazy like creating thousands of elements to a page the fastest way is to use document fragments. John Resig wrote a good blog post about it. http://ejohn.org/blog/dom-documentfragments/