Script Preparation code:
AخA
 
var testString = '<body><script>alert(1);</script foo="bar">' + Array(100001).join('<div>x</div>') + '</body>';
Tests:
  • CreateElement_div

    x
     
    function test_innerHTML() {
            var element = document.createElement('div');
            function decodeHTMLEntities(str) {
                if (str && typeof str === 'string') {
                    // strip script/html tags
                    str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
                    str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
                    element.innerHTML = str;
                    str = element.textContent;
                    element.textContent = '';
                }
                return str;
            }
            return decodeHTMLEntities(testString);
        }
  • DOMParser

     
    function test_DOMParser() {
        return (new DOMParser).parseFromString(testString, 'text/html').documentElement.innerText;
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    CreateElement_div
    DOMParser

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 28 days ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Chrome 133 on Windows
View result in a separate tab
Test name Executions per second
CreateElement_div 142890528.0 Ops/sec
DOMParser 157377296.0 Ops/sec