<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.`;
}
var html = "";
for(var i=0; i<1000; ++i){
html += `<p>${escapeHtml(`Hello ${i} & hello again.`)}</p>`;
}
dest.innerHTML = html;
dest.innerHTML = "";
for(var i=0; i<1000; ++i){
dest.insertAdjacentHTML("beforeend", "<p></p>");
dest.lastElementChild.textContent = `Hello ${i} & hello again.`;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
insertAdjacentHTML+textContent | |
escape+insertAdjacentHTML | |
insertAdjacentHTML+textContent w/ lastElementChild |
Test name | Executions per second |
---|---|
insertAdjacentHTML+textContent | 141.2 Ops/sec |
escape+insertAdjacentHTML | 517.9 Ops/sec |
insertAdjacentHTML+textContent w/ lastElementChild | 145.8 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark test case, which compares the performance of two approaches: using insertAdjacentHTML
with text content (textContent
) versus using escape()
followed by insertAdjacentHTML
. We'll break down each option, their pros and cons, and other considerations.
Benchmark Options
insertAdjacentHTML + textContent
insertAdjacentHTML()
method to insert HTML content into the DOM, followed by setting the textContent
property of the newly inserted element.escape() + insertAdjacentHTML
escape()
function to escape special characters in the text content, followed by using insertAdjacentHTML()
to insert the escaped HTML content into the DOM.Pros and Cons
insertAdjacentHTML + textContent
escape() + insertAdjacentHTML
Library: escape()
The escape()
function is a built-in JavaScript method that escapes special characters in a string to prevent XSS attacks. It replaces certain special characters with their corresponding HTML entities (e.g., &
becomes &
, <
becomes <
, etc.). This library is used to ensure safe insertion of user-provided content into the DOM.
Special JS Feature: insertAdjacentHTML
insertAdjacentHTML()
is a modern JavaScript method introduced in ECMAScript 2017 (ES7). It allows you to insert HTML content at specific positions within an element's DOM tree, without creating new elements or modifying existing ones. This approach is more efficient and flexible than using appendChild()
.
Other Considerations
insertAdjacentHTML
can provide better performance compared to appending new elements using appendChild()
.Alternative Approaches
Other alternatives to compare the performance of inserting HTML content into the DOM include:
appendChild()
: This approach involves creating new elements and appending them to an existing element's DOM tree.innerHTML
: This approach involves setting the innerHTML
property of an element, which can lead to security vulnerabilities due to XSS attacks.Keep in mind that these alternatives may have different performance characteristics and requirements for handling complex content or edge cases.