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

Revision 2 of this 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;
	const res = p.innerText;
	p.innerHTML = ''
	return res;
});
ready
Using Paragraph element, no cleanup
const p = document.createElement('p');
characters.forEach((s) => { 
	p.innerHTML = s;
	return p.innerText;
});
ready
Using Paragraph element with `textContent`
const p = document.createElement('p');
characters.forEach((s) => { 
	p.innerHTML = s;
	const res = p.textContent;
	p.innerHTML = ''
	return res;
});
ready

Revisions

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