testing the speed of using DOMParser versus a DOM element for sanitizing strings

Benchmark created on


Setup

const characters = ['>', '<', '&']

Test runner

Ready to run.

Testing in
TestOps/sec
Using Textarea element
const txt = document.createElement('textarea');
characters.forEach((s) => { 
	txt.innerHTML = s;
	return txt.value;
});
ready
Using DOMParser
const domParser = new DOMParser();

characters.forEach((s) => { return domParser.parseFromString(s, 'text/html').body.innerText; })
ready
Using Paragraph element
const p = document.createElement('p');
characters.forEach((s) => { 
	p.innerHTML = s;
	return p.innerText;
});
ready

Revisions

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