"Innerhtml += ..." VS "Appendchild(Txtnode)"

innerHTML += ... vs appendChild(txtNode)

The latter (appendChild) does not cause a complete rebuild of the DOM or even all of the elements/nodes within the target.

The former (setting innerHTML) does cause a complete rebuild of the content of the target element, which if you're appending is unnecessary.

Appending via innerHTML += content makes the browser run through all of the nodes in the element building an HTML string to give to the JavaScript layer. Your code then appends text to it and sets innerHTML, causing the browser to drop all of the old nodes in the target, re-parse all of that HTML, and build new nodes. So in that sense, it may not be efficient. (However, parsing HTML is what browsers do and they're really, really fast at it.)

Setting innerHTML does indeed invalidate any references to elements within the target element you may be holding -- because those elements don't exist anymore, you removed them and then put in new ones (that look very similar) when you set innerHTML.

In short, if you're appending, I'd use appendChild (or insertAdjacentHTML, see below). If you're replacing, there are very valid situations where using innerHTML is a better option than creating the tree yourself via the DOM API (speed being chief amongst them).

Finally, it's worth mentioning a couple of other alternatives:

  • append is a relatively recent addition to the DOM (but has excellent support other than truly obsolete browsers). It appends one or more items to the element, where the items can be nodes, elements, or HTML strings defining nodes and elements. Unlike appendChild, it supports HTML strings as well as nodes, and alsos supports multiple arguments. For your use case, it's less cumbersome than the next option in this list: parent.append(htmlString).
  • insertAdjacentHTML inserts nodes and elements you supply as an HTML string into or next to an element. You can append to an element with it: theElement.insertAdjacentHTML("beforeend", "the HTML goes here"); The first argument is where to put the HTML; your choices are:
    • "beforebegin" (outside the element, just in front of it)
    • "afterbegin" (inside the element, at the beginning)
    • "beforeend" (inside the element, at the end)
    • "afterend" (outside the element, just in after it)
      Note that "afterbegin" and "beforeend" insert into the element itself, whereas "beforebegin" and "afterend" insert into the element's parent. Browser support is basically universal.
  • insertAdjacentText is just like insertAdjacentHTML except that it inserts a text node (the text is not treated as HTML). Browser support is basically universal.

The difference between 'innerHTML' and 'appendChild'

Using appendChild adds a new DOM element to the end of the parent node.

Using innerHTML += takes the existing DOM content of the parent node, serialises it to HTML in a string, adds some more HTML to the end of the string, erases the existing elements in the parent node, generates DOM elements from that string, then appends the new nodes to the parent node.

Is creating element and set innerHTML more efficient or add HTML directly?

Best would be to make one big string in the loop, and assign the innerHTML once at the end.

let html = res.map(el => `<div class="..."> add json data in here </div>`).join('');
$mainBody.innerHTML = html;

The browser is very efficient at parsing HTML, but you should just do it once. Your method requires converting the DOM back to HTML and re-parsing it each time through the loop.

Performance/Goodness Design issue innerHTML += versus appendChild( li )

Appending to innerHTML will replace and re-parse the entire existing HTML structure.

It will lose user state (eg, focus), properties, event handlers, and any other state not in the HTML, and will be much slower.

You should never do that.

innerHTML += ... vs appendChild(txtNode)

The latter (appendChild) does not cause a complete rebuild of the DOM or even all of the elements/nodes within the target.

The former (setting innerHTML) does cause a complete rebuild of the content of the target element, which if you're appending is unnecessary.

Appending via innerHTML += content makes the browser run through all of the nodes in the element building an HTML string to give to the JavaScript layer. Your code then appends text to it and sets innerHTML, causing the browser to drop all of the old nodes in the target, re-parse all of that HTML, and build new nodes. So in that sense, it may not be efficient. (However, parsing HTML is what browsers do and they're really, really fast at it.)

Setting innerHTML does indeed invalidate any references to elements within the target element you may be holding -- because those elements don't exist anymore, you removed them and then put in new ones (that look very similar) when you set innerHTML.

In short, if you're appending, I'd use appendChild (or insertAdjacentHTML, see below). If you're replacing, there are very valid situations where using innerHTML is a better option than creating the tree yourself via the DOM API (speed being chief amongst them).

Finally, it's worth mentioning a couple of other alternatives:

  • append is a relatively recent addition to the DOM (but has excellent support other than truly obsolete browsers). It appends one or more items to the element, where the items can be nodes, elements, or HTML strings defining nodes and elements. Unlike appendChild, it supports HTML strings as well as nodes, and alsos supports multiple arguments. For your use case, it's less cumbersome than the next option in this list: parent.append(htmlString).
  • insertAdjacentHTML inserts nodes and elements you supply as an HTML string into or next to an element. You can append to an element with it: theElement.insertAdjacentHTML("beforeend", "the HTML goes here"); The first argument is where to put the HTML; your choices are:
    • "beforebegin" (outside the element, just in front of it)
    • "afterbegin" (inside the element, at the beginning)
    • "beforeend" (inside the element, at the end)
    • "afterend" (outside the element, just in after it)
      Note that "afterbegin" and "beforeend" insert into the element itself, whereas "beforebegin" and "afterend" insert into the element's parent. Browser support is basically universal.
  • insertAdjacentText is just like insertAdjacentHTML except that it inserts a text node (the text is not treated as HTML). Browser support is basically universal.

How do I append an HTML string containing many nodes directly to an existing DOM element with utmost performance in mind?

For append HTML string containing many nodes directly to an existing element you can use:

  1. insertAdjacentHTML (IE4, Ch1, FF8, Op7). It is best option and very simple.

  2. append, prepend, after, before, replaceWith (Ch54, FF49, Op39). Convenient API but works only in new browsers.

  3. Temporary node with outerHTML (IE4, Ch1, FF11, Op7). Before changing outerHTML you should append your node to some another node.

  4. Temporary node with innerHTML (all browsers) but you cannot delete this node from DOM so it is worst option.

  5. Parse HTML string using innerHTML or something else and append resulting nodes one by one to needed element (or all at once using methods below). This may be needed only for FF7- that does not support insertAdjacentHTML. But no one use such an old version.


For append many nodes (not a string) at once you can use:

  1. append, prepend, after, before, replaceWith (Ch54, FF49, Op39).

  2. DocumentFragment (all browsers). This is temporary container for nodes. When you append the container to some other node there is child nodes are appending not the container. After that the container will contains 0 children. It looks like the container disappears from DOM.

  3. appendChild or insertBefore many times (all browsers). It has worst performance.

  4. Calculate outerHTML of each node, concatenate results to one string and append this string using methods above. Performance is unknown so not recommended.



Related Topics



Leave a reply



Submit