44 points · by techdog · 17y ago · 23 comments
Terrible advice IMHO. True, innerHTML is faster in IE, so if you care about IE being slow (I don't), use innerHTML. However, innerHTML is buggy, has a bunch of caveats, and just looks incredibly ugly.
http://www.quirksmode.org/dom/innerhtml.html
On non-IE browsers, the difference in speed is negligible, and the code is far nicer using dom methods. (My browser SF/OSX reports 20ms for innerHTML, 25ms for DOM).
You also have useful references to nodes you might need later which makes code nicer and more optimum. For example, you could do innerHTML=foo, then use getElementByID later to update one of the nodes in foo. But using the dom methods, you can just save the ref to the node when you create it yourself. You can also attach any event handlers easily as you go, rather than trying to cram them in the HTML (Even more eugh) or set them up afterwards.
I agree that manipulating the DOM looks nicer. But if it saves 1 second of stalling, I'll use the innerHTML method any time.
As I posted :/ just click on the buttons and it'll test in your browser.
e.innerHTML = "Hello & world, here's some HTML <span>Some html</span>";
over e.nodeValue = "Hello & world, here's some HTML <span>Some html</span>";
Also you better be 100% sure you're data is 'safe' if you use innerHTML.
ege.innerHTML = "Hello "+some_user_provided_data+"!";
Are you suggesting that people develop two versions of their JS? One for IE and one for everyone else? Just to appease the pedantics of JS DOM manipulation?
For small bits of HTML, DOM creation/insertion is fine for reasons you have cited (attaching events, etc), but for a bunch of markup, sticking to your guns on this policy is going to cost you your job.
Making IE users experience worse also pushes people toward other browsers, which is a very good thing.
That is simply false. The average internet user should not have to make that leap of faith. They simply will think your website/app is crap and they will stop using it, because there's probably another product out there that does the same thing but actually works for them.
If you want to gain users and keep them, you do not make their experience intentionally bad.
I've become a big fan of JST: http://code.google.com/p/trimpath/wiki/JavaScriptTemplates . Send over template snippets as strings and use JST to put them together. With far fewer innerHTML replacements along the way.
If I need a complex algorithm that will run once a month and someone has already written an open source solution, then sure.
But if I need something that must be efficient and runs thousands of times per day, then maybe I should roll my own.
I guess the formula could be:
WriteYouOwn if DevTimeSaved < (TxnTimeSaved * TxnsPerDay)
That said, constructing HTML as a string and using one innerHTML call is much faster than doing createNode/appendChild multiple times. DOM operations are definitely the bottleneck when it comes to performance : The less of them you do, the better off you are. (I learnt my lesson the hard way)
http://ejohn.org/blog/dom-documentfragments/ http://slayeroffice.com/articles/innerHTML_alternatives/#5c