DOMParser vs innerHTML

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Using DOMParser
const stripHtmlTags = (text) => {
  let strippedText;
  try {
    strippedText = new DOMParser().parseFromString(text, 'text/html').body.textContent;
  } catch (e) {
    console.error('Failed to strip HTML markup from string', e);
    strippedText = text;
  }
  return strippedText;
}
stripHtmlTags('Test <strong>Units: 2</strong>');
ready
Using temp DOM element textContent
const stripHtmlTags = (text) => {
  let strippedText;
  try {
  	const tmp = document.createElement('div');
    document.innerHTML = text;
    strippedText = document.textContent.trim();
  } catch (e) {
    console.error('Failed to strip HTML markup from string', e);
    strippedText = text;
  }
  return strippedText;
}
stripHtmlTags('Test <strong>Units: 2</strong>');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.