javascript - Static HTML or generate Dynamic HTML | performance -
i want know method faster/better , why.
i have web app. has lot of html tags like:
<div id="info-view"> <h3 class="h3-title">info</h3> <div class="card"> <h4>version</h4> <div class="version"> <p class="list-item">app version: <span id="app-vr"></span> ...so on </div> </div> </div> <div id="import-view"> <h3 class="h3-title">import</h3> <div class="card"> <h4>import list</h4> <div id="import-container"> <div class="import-data"> <p class="import-name">data bla blabla</p> </div> <div class="import-data"> ... </div> ... </div> </div> </div> <!-- , lot of other view container these...online-view...setting-view... -->
and everytime need view shown, put style.display current 1 'none' , view need 'block'.
now question is: better generate div-blocks in javascript? like:
// remove current content dom while(document.body.firstchild) { document.body.firstchild.remove(); } // generate new content var infoview = document.createelement("div"); infoview.id = "info-view"; var title = document.createelement("h3"); title.classlist.add("he-title"); title.textcontent = "info"; ... // add dom document.body.appendchild(infoview);
if need view remove current 1 completly dom , generate new 1 , display it.
which 1 better performance/readability/code maintenance ?
because in code lot dom.
you can use https://jsperf.com check dom manipulation performance.
for case performace measurement shows adding/removing elements faster operation showing/hiding in chrome.
you can rerun test in different browsers measure operation , create different flows based on browser type use better alternative.
Comments
Post a Comment