Skip to content

Instantly share code, notes, and snippets.

@ryanmorr
Created January 29, 2024 07:56
Show Gist options
  • Save ryanmorr/21f143615cab7e805145108b943ed0c8 to your computer and use it in GitHub Desktop.
Save ryanmorr/21f143615cab7e805145108b943ed0c8 to your computer and use it in GitHub Desktop.
Parse an HTML string into a document fragment
const cache = new Map();
const template = document.createElement('template');
function parseHTML(html) {
if (cache.has(html)) {
return cache.get(html).cloneNode(true);
}
template.innerHTML = html;
cache.set(html, template.content);
return cache.get(html).cloneNode(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment