Flagrant Badassery

A JavaScript and regular expression centric blog

When innerHTML isn’t Fast Enough

This post isn't about the pros and cons of innerHTML vs. W3C DOM methods. That has been hashed and rehashed elsewhere. Instead, I'll show how you can combine the use of innerHTML and DOM methods to make your code potentially hundreds of times faster than innerHTML on its own, when working with large numbers of elements.

In some browsers (most notably, Firefox), although innerHTML is generally much faster than DOM methods, it spends a disproportionate amount of time clearing out existing elements vs. creating new ones. Knowing this, we can combine the speed of destroying elements by removing their parent using the standard DOM methods with creating new elements using innerHTML. (This technique is something I discovered during the development of RegexPal, and is one of its two main performance optimizations. The other is one-shot markup generation for match highlighting, which avoids needing to loop over matches or reference them individually.)

The code:

function replaceHtml(el, html) {
	var oldEl = typeof el === "string" ? document.getElementById(el) : el;
	/*@cc_on // Pure innerHTML is slightly faster in IE
		oldEl.innerHTML = html;
		return oldEl;
	@*/
	var newEl = oldEl.cloneNode(false);
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	/* Since we just removed the old element from the DOM, return a reference
	to the new element, which can be used to restore variable references. */
	return newEl;
};

You can use the above as el = replaceHtml(el, newHtml) instead of el.innerHTML = newHtml.

innerHTML is already pretty fast...is this really warranted?

That depends on how many elements you're overwriting. In RegexPal, every keydown event potentially triggers the destruction and creation of thousands of elements (in order to make the syntax and match highlighting work). In such cases, the above approach has enormous positive impact. Even something as simple as el.innerHTML += str or el.innerHTML = "" could be a performance disaster if the element you're updating happens to have a few thousand children.

I've created a page which allows you to easily test the performance difference of innerHTML and my replaceHtml function with various numbers of elements. Make sure to try it out in a few browsers for comparison. Following are a couple examples of typical results from Firefox 2.0.0.6 on my system:

1000 elements...
innerHTML (destroy only): 156ms
innerHTML (create only): 15ms
innerHTML (destroy & create): 172ms
replaceHtml (destroy only): 0ms (faster)
replaceHtml (create only): 15ms (~ same speed)
replaceHtml (destroy & create): 15ms (11.5x faster)

15000 elements...
innerHTML (destroy only): 14703ms
innerHTML (create only): 250ms
innerHTML (destroy & create): 14922ms
replaceHtml (destroy only): 31ms (474.3x faster)
replaceHtml (create only): 250ms (~ same speed)
replaceHtml (destroy & create): 297ms (50.2x faster)

I think the numbers speak for themselves. Comparable performance improvements can also be seen in Safari. In Opera, replaceHtml is still typically faster than innerHTML, but by a narrower margin. In IE, simple use of innerHTML is typically faster than mixing it with DOM methods, but not by nearly the same kinds of margins as you can see above. Nevertheless, IE's conditional compilation feature is used to avoid the relatively minor performance penalty, by just using innerHTML with that browser.

There Are 113 Responses So Far. »

  1. ??? ???????,??? ?? ???,??? ?? ???????????????? ??????? ??,??? ??????? ????????????,??? ??????? ??,???? ??????? ??,????????????,?????????,??????????????????????????????????,????,?????????,?????????,??????????????,????????????????,??????????,D?G,???????,???????,??,???????,,?,??????????,????????,????????,?????????,????????? ????????? ???????????? ??? ????????? ??? ??? ?MAX80?OFF?????SALE?????????????????? ???,?????? ??? ??,?????? ??? ??? http://www.msnbrand.com/brand-copy-IP-13.html

  2. ???????????????????????????????????????????? ??????????????????????????????????????????????9????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? http://www.okakaku.com/brand-39-copy-0.html

  3. ???????????????????????????????????????????????????????????????????????? ????????????????????????????????????IWC????????????????????????????????????????????????????????????? 2015????????????????10% ? ????????! ? ?????????????? ? 100%?????? ? ???????(????)!? ????????????? ????: ??????????????????????????????????????? http://www.newkakaku.com/ldb1.htm

  4. 2015?????-??!??? ????????? ?N?????????????????????????????HERMES??????????? CHANEL???????????LOUIS VUITTON?????????????? BVLGARI???????Christian Dior???????? COACH????????GUCCI???????? ROLEX????OMEGA???? IWC????FRANCK MULLER???? HUBLOT??????? CHLOE?????????????? ????????????????????? http://www.ooowatch.com/tokei/zenith/index.html

  5. ??????????????????,????????,???????? ??? ?? ???? ??(CHANEL),??? ?? (GUCCI) ??,????(lv) ?? ?? ?? ???????????,?? ????????????????????????????????????????????????????????????,??????????,???????,??????????????,??????? ??? ???????? ?? ???????? ????? ??? ??????? ????????????????????????????????? ??????????????????????.????????????????????? ????????????????????IWC???????????? ??????????? http://www.gowatchs.com/brand-208.html

  6. ???????????????????????????????????????????????????SS??N?????????????2015????????????????????(????)????????????CHH67723(*^^*)11??????????(*^^*)?????????????????CH783283????? ??????????????????? ?????????? ?????????? ??????????????????????? ??????? ??? http://www.bagkakaku.com/hermes_bag.html

  7. ????????????????????????????????????????????????????????????????????????????????????N?????????????? ?????????????????????,??????????,??????,??????? ,??????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????? ???????????? ??????????????????????????????????????????????????????????????? ????????????????????????? ????????gaga????????, ?/????,??????????O????????????????????????????????????????live??????????????????????????????????????O????????????? http://www.eevance.com/index.html

  8. We’re excited to introduce you to the first released application on HackMeGame – Boom Beach
    hack data file android Hack Instrument!

  9. If some one needs expert view regarding running a blog afterward i advise him/her to
    pay a quick visit this blog, Keep up the good
    work.

  10. ??? ???????????????????????????????????????????? ???????????????????????????????????????????? ??????????????????????????????????????????????????????? http://www.bestevance.com/rolex/mirugausu/index.htm

  11. ?????????????????????????????????????????????????? CHANEL ??????????? ??????????? ??? ??????????????????????????????????????????????????????????????????????????? ????????????????????????? ????? ???????????????? http://www.ooobag.com/wallet/louisvuitton/index_10.html

  12. ??????,??????,???????,????????????? ????????????????????????????1854??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????LV??????????????????????????????????LV????????Louis Vuitton???????????????????????????????????? http://www.ooobag.com/wallet/louisvuitton/index_8.html

  13. ?????????????????????N??????100%????????????????????????????,??????+?????+???????+?????+???????????????????????????????? http://www.gginza.com/%E6%99%82%E8%A8%88/%E3%83%AD%E3%83%AC%E3%83%83%E3%82%AF%E3%82%B9/daytona/8cc724a5a792d955.html

Post a Response

If you are about to post code, please escape your HTML entities (&, >, <).