<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.`;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
insertAdjacentHTML+textContent | |
escape+insertAdjacentHTML | |
insertAdjacentHTML+textContent w/ lastElementChild |
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 |
I'll explain the benchmark in detail.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark test created on MeasureThat.net. The test compares three different approaches for inserting HTML content into an element: insertAdjacentHTML
with textContent
, escape
function, and lastElementChild
.
Script Preparation Code
The script preparation code is as follows:
const dest = document.getElementById('dest');
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}
This code creates a dest
element and defines an escapeHtml
function. The escapeHtml
function takes an input string unsafe
and escapes its special characters to HTML-safe format using regular expressions.
HTML Preparation Code
The HTML preparation code is as follows:
<div id='dest'></div>
This code creates a simple div
element with the ID dest
.
Benchmark Test Cases
There are three test cases:
insertAdjacentHTML
to insert HTML content into the dest
element and then sets the textContent
of the last child element.insertAdjacentHTML
with the escaped input string using the escapeHtml
function and then sets the text content of the last element child.lastElementChild
instead of lastChild
to set the text content.Options Compared
The three test cases compare the performance of different approaches for inserting HTML content into an element:
insertAdjacentHTML
to insert HTML content and then sets the text content of the last child element.insertAdjacentHTML
with the escaped input string using the escapeHtml
function and then sets the text content of the last element child.lastElementChild
instead of lastChild
.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
insertAdjacentHTML
directly, as it escapes special characters.Other Considerations
The benchmark results show that the execution speed for insertAdjacentHTML+textContent
and insertAdjacentHTML+textContent w/ lastElementChild
are relatively close, suggesting that the choice between these two approaches may not have a significant impact on performance. However, using the escapeHtml
function introduces additional overhead, which may affect the overall performance of the application.
Alternative Approaches
Other alternatives to consider when inserting HTML content into an element include:
html()
method, which provides a more concise way of setting the innerHTML of an element.Keep in mind that the choice of approach depends on the specific use case and requirements of your application.