LightningDOM to Balance DOM vs innerHTML 09.12.08
Using DOM API versus innerHTML to create/change content on webpage has been in discussion for years. The pain (slow DOM manipulation speed) left by Internet Explorer causes a big concern over DOM API for serious use in practice because the browser is still holding big slice of market share today (about 80%).
We can guess that the browser war (re)started by Firefox and stirred up by Google Chrome lately will result in better IE versions, yet that bright day won’t come any time soon.
Recently, I had reviews on PURE and Chain.js as client-side template engines. Though both libraries take pure, unobtrusive JavaScript approach for implementation, PURE seems more eager about speed when using innerHTML to create elements while Chain.js depends on DOM API for more flexible and direct manipulation.
I cannot say what approach is better. PURE may have some advantage on page with huge number of DOM elements to be created/changed but I think most of projects will be happy with Chain.js for its power. It grows a question: any chance that we can achieve both flexibility and speed in these libs? Probably, LightningDOM donated by Peter Rust will be a right answer.
In the post, Peter places LightningDOM as “a layer between the raw HTML and your templating/data-mapping code, without the slowness of the DOM.” So how it works? Just see the example.
1 2 3 4 | var strMarkup = document.getElementById('destination').innerHTML; var dom = new LightningDOM(strMarkup); dom.firstChild().innerText("I Love speed!"); document.getElementById('destination').innerHTML = dom.outerHTML(); |
Not too difficult to understand, right? You give all HTML markups to LightningDOM, do anything you want with familiar getters/setters like you can do with DOM API then assign the result back to real DOM’s innerHTML.
Peter tells more about internal implementation to get both speed and lightness in the library.
The LightningDOM parses the given HTML using Regexes. I have two different parse algorithms (but both using the same regex). IE is much faster at parsing the whole markup by doing a single regex replace to convert the markup into JSON and then to eval the JSON. FF (and I’m guessing Chrome & Opera) is much faster at looping through the markup with a global regex.
All the tags are parsed into a two-dimensional array (well, technically an array of arrays), which represents all the information in the tags. Here’s a pretty look at the internal data structure representing a chunk of HMTL (note that we use a simpler object model inspired by ElementTree to representing the inner-text as a “tail” of the previous tag, rather than a nested “Text Node”).
Array of Arrays (_aMarkup):
Of course, the decision to utilize LightningDOM for PURE or Chain.js depends on leaders of projects but it does not prevent you from taking advantage of the lib if you see true value from it. Personally, I think it’s a good solution though I’ll have more tests/benchmarks to evaluate its stability across browsers.

Professional Web 2.0 Programming (Wrox Professional Guides)
Accelerated DOM Scripting with Ajax, APIs, and Libraries













September 12th, 2008 at 8:41 am
Thanks for the post, Jimmy Vu!
One slight typo: I think you mean “guess” in the second paragraph, not “guest”.
Please let me know if you find any bugs or instability in your testing.
September 12th, 2008 at 10:08 am
[...] Jimmy Vu says that PURE (and LightningDOM, by extension) have an advantage when you have a “huge number of DOM elements to be created/changed” and he implies that this is uncommon. I think the appropriate word is “significant”. For a web app that uses client-side templating exclusively, this is standard — as it should be. Take a well-designed app like Basecamp. The average page has 50+ complex items displayed, each requiring a minimum of 3 HTML elements to represent them. [...]
September 12th, 2008 at 10:09 am
I take it this doesn’t fix IE bugs with .innerHTML though, as it still uses this to set the final result.
E.g. in IE you can’t set the innerHTML of a SELECT element, or TABLE, THEAD, TR etc.
Table issues
http://webbugtrack.blogspot.com/2007/12/bug-210-no-innerhtml-support-on-tables.html
and
Select issues
http://webbugtrack.blogspot.com/2007/08/bug-274-dom-methods-on-select-lists.html
IE also has issues with PRE elements and certain combos of CSS positioning.
I guess this is just a heads up to anyone unaware of these bugs)
September 13th, 2008 at 8:34 pm
@Peter: Thanks for pointing out the typo.
I agree that DOM would be almost useless with “significant” number of elements, yet it’s still OK with less than 100 ones. I wish Rizqi will seriously consider LightningDOM for Chain.js or similar approach for better performance.