Skip to content

Instantly share code, notes, and snippets.

@bluebrown
Created June 8, 2021 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluebrown/7f8ddccdf7680c8fa68f3ee92203d7ef to your computer and use it in GitHub Desktop.
Save bluebrown/7f8ddccdf7680c8fa68f3ee92203d7ef to your computer and use it in GitHub Desktop.
custom innerHTML function with improved performance
// taken from below url, which explains the why this function makes sense
// https://blog.stevenlevithan.com/archives/faster-than-innerhtml
function replaceHtml(el, html) {
var oldEl = typeof el === "string" ? document.getElementById(el) : el;
/*@cc_on // Pure innerHTML is slightly faster in IE
oldEl.innerHTML = html;
return oldEl;
@*/
var newEl = oldEl.cloneNode(false);
newEl.innerHTML = html;
oldEl.parentNode.replaceChild(newEl, oldEl);
/* Since we just removed the old element from the DOM, return a reference
to the new element, which can be used to restore variable references. */
return newEl;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment