html - Efficient Javascript for DOM modifications -
say, i've got html file want process using javascript. example:
- add dom elements,
spanordivwrappers. - change document styling bit, base font size, line-height, etc.
- use hyphentor add
­entities.
what efficient way this, i.e. i'd minimum amount of reflows.
the ideal case having js-code run before first layout. possible? know, it's bad idea execute expensive scripts before page has been displayed make page blank time , it's bad experience. however, i'm going need work offline , not issue project.
or, there's way dom modifications in 1 bunch, i.e. reflow triggered after alterations have finished?
there several ways. primary goal of them end causing single - or @ least few possible - reflows/repaints.
off-dom element
you can work elements without appending dom, , append once everything's set up.
the issue if code needs reference offset dimensions, since elements not yet in dom not have any.
var container = document.createelement('section'); //not in dom yet //do extensive work here prepare doc document.body.appendchild(container); //now go dom, causing single re-paint doc fragment
a more formalised, , possible improved, variation on theme document fragments, bring performance benefit (apparently). they're second, separate dom has no display.
string
probably quickest of of these build document string. obvious implication can't use dom methods on it, if you're happy string-handling , regex'ing (not ideal on html, of course), worth considering.
finally, if speed of essence , have lot of computation or string-handling do, might want harness web workers. these can't talk dom directly, can outsource tasks them and, crucially, work asynchronously on separate thread.
Comments
Post a Comment