3 ms·
This is pretty, but unfortunately the only performant way to generate dom elements in javascript is with HTML. HTML parsing is orders of magnitude faster in mos
by aneth 14y ago
This is pretty, but unfortunately the only performant way to generate dom elements in javascript is with HTML. HTML parsing is orders of magnitude faster in most browsers.
How about a library like this to generate a string of HTML instead of dom elements?
- mistercow 14y agoSeems to me like an implementation detail that shouldn't have to affect the interface at all.
- firefoxman1 14y agoLike a template engine?
- aneth 14y agoPerhaps like some template engine, but not like any I know of.
- joestelmach 14y agoWhile I don't entirely agree with your first statement, you could just grab the outerHTML instead of appending the element directly to the DOM: $.el.div($.el.span()).outerHTML
- aneth 14y agoSeems like a reasonable solution. My first statement was certainly true in the previous generation of browsers, including IE 7. I'm not sure about more modern browsers. What basis do you have for disagreeing? Refer to: http://www.quirksmode.org/dom/innerhtml.html http://www.quirksmode.org/dom/innerhtml.html
- joestelmach 14y agoI'm only disagreeing with the use of the word 'performant', and the popular viewpoint that any solution that isn't as fast as possible can't be labeled as such.
- Stratoscope 14y agoI tried that some years ago (see my other comment). I had a very similar DOM creation function and I updated it to generate HTML instead. It was almost as slow as before - and then I realized that the sheer amount of JS code I was running to generate the HTML was taking up the time. Of course the performance tradeoff would be different in modern browsers.
- betageek 14y agoI started one in 2009 (https://github.com/dburrows/markup-builder https://github.com/dburrows/markup-builder) inspired by Ruby's Builder library but abandoned it - it was only really useful for small amounts of code and for any sizeable project it's far better to use templates