0

I have a two functions from this answer to another question:

1.

const destroy = container => {
  document.getElementById(container).innerHTML = '';
};
const destroyFast = container => {
  const el = document.getElementById(container);
  while (el.firstChild) el.removeChild(el.firstChild);
};

Why second func is faster than first func?

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
Windows Eight
  • 43
  • 1
  • 7

1 Answers1

4

As a rule of thumb, innerHtml is slow because the browser needs to reparse the html (even though it is being set to ''). removeChild, however, directly modifies the DOM without any parsing needed. Therefore removeChild is faster.

Luke
  • 565
  • 5
  • 19