Test name | Executions per second |
---|---|
insertAdjacentHTML+textContent | 150.9 Ops/sec |
escape+insertAdjacentHTML | 150.5 Ops/sec |
insertAdjacentHTML+textContent w/ lastElementChild | 149.3 Ops/sec |
<div id='dest'></div>
const dest = document.getElementById('dest');
function escapeHtml(unsafe)
{
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}
dest.innerHTML = "";
for(var i=0; i<1000; ++i){
dest.insertAdjacentHTML("beforeend", "<p></p>");
dest.lastChild.textContent = `Hello ${i} & hello again.`;
}
dest.innerHTML = "";
for(var i=0; i<1000; ++i){
dest.insertAdjacentHTML("beforeend", `<p>${escapeHtml(`Hello ${i} & hello again.`)}</p>`);
}
dest.innerHTML = "";
for(var i=0; i<1000; ++i){
dest.insertAdjacentHTML("beforeend", "<p></p>");
dest.lastElementChild.textContent = `Hello ${i} & hello again.`;
}