Yes, elm.innerHTML += str;
is a very bad idea.
Use elm.insertAdjacentHTML( 'beforeend', str )
as the perfect alternative.
The typical "browser has to rebuild DOM" answer really doesn't do the question justice:
First the browser need to go through each elements under elm, each of their properties, and all their texts & comments & process nodes, and escape them to build you a string.
Then you have a long string, which you append to. This step is ok.
Third, when you set innerHTML, browser has to remove all the elements, properties, and nodes it just went through.
Then it parse the string, build from all the elements, properties, and nodes it just destroyed, to create a new DOM fragment that is mostly identical.
Finally it attach the new nodes, and the browser has to layout the whole thing. This may be avoidable (see the alternative below), but even if the appended node(s) requires a layout, old nodes would have their layout properties cached instead of re-calculated from fresh.
But it's not done yet! The browser also have to recycle old nodes by scanning all javascript variables.
Problems:
Some properties may not be reflected by HTML, for example the current value of <input>
will be lost and reset to the initial value in the HTML.
If you have any event handlers on the old nodes, they will be destroyed and you have to reattach all of them.
If your js code is referencing any old nodes, they will not be destroyed but will instead be orphaned.
They belong to the document but is no longer in the DOM tree.
When your code access them, nothing may happen or it may throw error.
Both problems mean it is unfriendly with js plugins - the plugins may attach handlers, or keep reference to old nodes and cause memory leak.
If you get into the habit of doing DOM manipulation with innerHTML, you may accidentally change properties or do other things you didn't want to.
The more nodes you have, the more inefficient this is, the more battery juice for nothing.
In short, it is inefficient, it is error prone, it is simply lazy and uninformed.
The best alternative is Element.insertAdjacentHTML
, that I haven't seen other answers mention:
elm.insertAdjacentHTML( 'beforeend', str )
Almost same code, without innerHTML's problems.
No rebuild, no handler lost, no input reset, less memory fragmentation, no bad habit, no manual element creations and assignments.
It allows you to inject html string into elements in one line, including properties, and even allows yow to inject composite and multiple elements.
Its speed is optimised -
in Mozilla's test it is 150 times faster.
In case someone tell you it is not cross browser, it is so useful that it is HTML5 standard and available on all browsers.
Don't ever write elm.innerHTML+=
again.
element.innerHTML +=
? – Friedafriedberg<script>
within the element will be rerun in some browsers. Finally, there is no guarantee thatelm.innerHTML = elm.innerHTML
will always reproduce an identical copy of the element's DOM. – Abie