The need for speed: innerHTML versus DOM manipulation

July 3, 2007, 9:21 pm · 0 comments · Filed under: JavaScript, Web Development

At work, I’ve been tackling some fairly sophisticated JavaScript DOM manipulation scenarios. As such, I’ve started to run up against the limitations of my long-used technique of stuffing loads of HTML as a string into some element’s innerHTML property. Specifically, when inserting very long strings containing complex HTML, there can be a troublesome delay between when JavaScript thinks it’s done inserting the elements and when the elements are actually available for further manipulation.

andrew.hedges.name



Can your web browser do this?

You’ll never get rich digging a ditch, nor building Dashboard widgets.

A Kryptonite™ lock can be defeated in 11 seconds, but you still lock your bike, right?

Gaining Twitter followers is a little like losing weight. You have to try.

Over or under? It’s the age-old question when it comes to the orientation of toilet paper rolls.

Subscribe


Meta Me

I am a web developer, recently returned to the States after 3 years in New Zealand. I’m into my family, photography and frisbee sports.

Blip.fm Digg Facebook LinkedIn Stack Overflow Twitter Zooomr

Nothing will benefit human health and increase chances for survival of life on earth as much as the evolution to a vegetarian diet.
Albert Einstein


Topics

Apple · AppleScript · Business · Coda · CSS · Dashboard · Design · Google · InSTEDD · JavaScript · jQuery · Life · Marketing · Music · New Mexico · New Zealand · Open Source Software · Photography · PHP · Politics · Ruby on Rails · Scree · Subversion (SVN) · Twitter · Usability · Web Development · Widgets


Archives


Most Popular

CSS Fast Nav: Because (perception of) speed matters! · Personal Branding for Introverts · Stupid WebKit Tricks · Add an interactive legend to a MarkerManager managed Google Map · Dude. Mikeyy can’t even spell his own name. · Dashboard Widgets for Fun and Profit · Animating your iPhone web application · How-to recover from checksum mismatch errors in SVN · Why Apple can afford to charge so little for Snow Leopard · The first 48 hours of PHP Function Reference, by the numbers


Most Recent

CSS Fast Nav: Because (perception of) speed matters! · When is a global variable not a variable? · Our misguided culture of cool · InSTEDD: Open Source Software that saves lives · Add an interactive legend to a MarkerManager managed Google Map · Personal Branding for Introverts · Moments of Rangitoto · Some Twitter conventions · Why Apple can afford to charge so little for Snow Leopard · Stupid WebKit Tricks


Twitshirt

Twitshirt is a tweet on a shirt. Buy the one below or check out my most recent tweets.

I just mis-typed "meta" as "meat". 3 times. I can see the headline now: "Vegetarian Caught in Freudian Slip" Film at 11.

See a random Twitshirt-worthy tweet.


Friends

80/20 · 90 Seven Design · Alyson Hurt · Andrew Nimick · Apps & Hats · Ben Young · Brian Arnold · Brian Warren · Carl Bolter · Chris Burgess · Christine Morris · Cristina Stoian · Daniel Lyons · Daniel Schwartz · David Hedges · Hamish Campbell · Jochen Daum · John Visser · Joseph McLaughlin · Joshua Sallach · Julian Pistorius · Justine Sanderson · Kalena Jordan · Katie Graham · Kelly Green · Kevin Potis · Mark Bixby · Matt Henry · Method Arts · Morgan Pyne · Peter Michaux · Philip Tellis · Piers Harding · Rebecca Murphey · Reid Givens · Rey Bango · Rhett Anderson · Richard Paul · Rob Pongsajapan · Robin Taylor · Ryan Park · Shaun Lee · Simon Young · Su Yin Khoo · Toni Barrett · Vaughan Rowsell · Vincent Thomé · Voom Studio


Recommended Books on
Web Development

My bias is for references over “cookbooks.” I want to know all of my options, not just one way to do something. Show me the why as well as the how and I am happy.

JavaScript: The Good Parts · Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries · JavaScript: The Definitive Guide · Designing with Web Standards · CSS: The Definitive Guide · Prioritizing Web Usability · The Elements of User Experience · Web ReDesign: Workflow that Works · Don't Make Me Think: A Common Sense Approach to Web Usability


Contact Info

Contact info for Andrew Hedges


I’ve hosted this website with pair Networks since 1997. They rock.

This blog is powered by…software I wrote.

Feeling generous? Knock yourself out!

After reading some articles on the subject, I conducted my own tests (go there to see some code!) of the speed of innerHTML versus direct DOM manipulation (in the process coming up with highly optimized versions of each.) I came to the conclusion that there is a bit of mythology on the Intertubes that may be based on incomplete knowledge of how DOM methods work.

Myth:
innerHTML execution is way faster than DOM scripting.
Truth:
When written optimally, DOM scripting ranges from clearly slower (run in Internet Explorer 6 and 7) to nearly as fast (on Gecko-based browsers) to slightly faster (on Safari and Opera). This also only takes into account the time it takes the browser to release the thread, not how long it takes for the new node(s) to be available for manipulation. Saying “[innerHTML] is faster than DOM. This is a proven fact.” is simply bogus.
Myth:
innerHTML is way easier to code up.
Truth:
There is some truth to this. For a novice scripter, if you have haven’t dealt much with XML or the DOM, it can be confusing to do more than just dump strings into innerHTML. But there are advantages to going the hard road. A big one (for me) is that innerHTML is not technically a standard so it offends my inner purist support for it could dwindle over time. More objectively, it does take time to attach nodes to the DOM when using innerHTML whereas nodes attached directly are available immediately. While not a problem in a lot of use cases, it’s cropped up as a problem for me in certain situations.
Myth:
innerHTML takes way less code to do the same thing.
Truth:
innerHTML takes way less code to do the same thing. So what? If innerHTML is causing problems that DOM scripting can solve, are you really so lazy that you’re going to avoid it anyway? I mean, isn’t that why it’s called work?

To be fair, there are some gotchas that are worth noting with DOM scripting. Namely, you can’t use HTML entities in text nodes (makes sense, but I learned the hard way). Instead, you have to use Unicode encoded entities. For example, rather than using ’ for an apostrophe, you have to use \u2019. Luckily, there are sites on the Interweb that let you look this stuff up.

The rest (at least in terms of optimizations) is common sense to most programmers, e.g., do as much work outside your loops as possible. One tip that I found really sped things up was to work on a document fragment, rather than directly manipulating the top level document object.

Conclusion

Any anthropologist will tell you that myths don’t have to be true to be useful. Maybe it’s better that JavaScript programmers don’t go the DOM route if they don’t fully understand how to make it work effectively. My advice? Bite the bullet and learn how to (efficiently) script the DOM.

Update: I should probably have been more explicit about this above, but you can get to the code I used to run my speed tests here.

Update 2009-12-25: After some discussion on the iUI Google Group, I updated my speed test to use PPK’s method for measuring DOM attachments. My conclusions above are unchanged.

This entry was first published on Newfangled Telegraph, my former freelancing website.

Short URL to this article:
Tweet this article!


Comments close automatically after 90 days.
Still have something to say? Drop me a line!

Possibly related posts