Hacker News new | ask | show | jobs
by aneth 5174 days 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?

5 comments

I 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.

While 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
Seems 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

I'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.
I started one in 2009 (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
Seems to me like an implementation detail that shouldn't have to affect the interface at all.
Like a template engine?
Perhaps like some template engine, but not like any I know of.