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