HTML Preparation code:
x
 
1
2
<div id='dest'></div>
Script Preparation code:
 
const dest = document.getElementById('dest');
function escapeHtml(unsafe)
{
    return unsafe
         .replace(/&/g, "&amp;")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;");
 }
Tests:
  • insertAdjacentHTML+textContent

     
    dest.innerHTML = "";
    for(var i=0; i<1000; ++i){
      dest.insertAdjacentHTML("beforeend", "<p></p>");
      dest.lastChild.textContent = `Hello ${i} & hello again.`;
    }
  • escape+insertAdjacentHTML

     
    dest.innerHTML = "";
    for(var i=0; i<1000; ++i){
       dest.insertAdjacentHTML("beforeend", `<p>${escapeHtml(`Hello ${i} & hello again.`)}</p>`);
    }
  • insertAdjacentHTML+textContent w/ lastElementChild

     
    dest.innerHTML = "";
    for(var i=0; i<1000; ++i){
      dest.insertAdjacentHTML("beforeend", "<p></p>");
      dest.lastElementChild.textContent = `Hello ${i} & hello again.`;
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    insertAdjacentHTML+textContent
    escape+insertAdjacentHTML
    insertAdjacentHTML+textContent w/ lastElementChild

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 2 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.53
Chrome 101 on Windows
View result in a separate tab
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