According to spirit of stackoverflow (thanks to @Bill the Lizard clarifying this) I would like to introduce small own research of the well known to all front-end web developers question.
How to get better performance of jscript while programming DHTML, using innerHTML or DOM operations?
For the testing I have created single HTML page with “start test” button that runs 5000 times pair of function, that is written either on innerHTML ideology, or by DOM. (It can be taken on: http://codepad.org/HyiKRsNk )
- To avoid compare CPU result are displayed in percent, how one of function is faster then another. So if you see (17%) it is mean that DOM faster innerHTML, if (may be latter) you see (-5%) it is mean innerHTML faster than DOM.
- To avoid effect of memory blow-up, for each call new "test area" is created and deleted immediately after test (time for creation/deletion is not encountered).
- Test are performed under display:none div to avoid flipping and performance degradation on scrolling
I have prepared 3 test (the last one proposed by @tomdemuyt)
create new DIV and child text for it
remove some DIV (to avoid influence of creation difference, both tests uses same way to create test div, but different for removing)
adding onclick event handler
- Test creation of tree in depth, test create
<table><tbody><tr><td><span>text
construction. DOM test uses sequential calls of pair createElement/appendChild Similar as (4) but 2 rows
<tr>
are created. DOM uses special helper function to simplify:treeHelper("table", treeHelper("tbody", treeHelper("tr",
Test results are followed for different browsers.
So dear colleagues please provide: show some test when innerHTML is faster, start my test on non-listed-yet browsers.