box1 = document.createElement("div");
box2 = document.createElement("div");
box2.src = "https://google.com";
box2.setAttribute('style', 'position:absolute;display:block;width:300px;hright:300px;border:2px solid blue');
box2.textContent = "X";
for (var i=0; i<100;i++){
box1.innerHTML += box2.outerHTML;
}
for (var i=0; i<100;i++){
box1.insertAdjacentHTML('beforeend', box2.outerHTML);
}
for (var i=0; i<100;i++){
let El = document.createElement("div");
El.src="https://google.com";
El.setAttribute('style','position:absolute;display:block;width:300px;hright:300px;border:2px solid blue');
El.textContent = "X";
box1.appendChild(El);
}
for (var i=0; i<100;i++){
let El = document.createElement("div");
El.src = "https://google.com";
El.setAttribute('style', 'position:absolute;display:block;width:300px;hright:300px;border:2px solid blue');
El.textContent = "X";
box1.insertAdjacentElement('beforeend', El);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerHTML | |
insertAdjacentHTML | |
appendChild | |
insertAdjacentElement |
Test name | Executions per second |
---|---|
innerHTML | 2.6 Ops/sec |
insertAdjacentHTML | 1672.4 Ops/sec |
appendChild | 2385.0 Ops/sec |
insertAdjacentElement | 2429.6 Ops/sec |
Let's break down what's being tested on MeasureThat.net.
Benchmark Overview
The benchmark compares the performance of four different methods for adding or inserting HTML elements into an existing element:
innerHTML
insertAdjacentHTML
appendChild
insertAdjacentElement
Each method is compared in a loop that runs 100 times, with the same input data (a div
element with some inline styles and text content).
Options Compared
The four options are compared as follows:
innerHTML
property.insertAdjacentElement
.insertAdjacentHTML
, but takes an individual HTML element as an argument instead of a fragment.Pros and Cons of Each Approach
Here are some pros and cons of each approach:
innerHTML
for large amounts of data, as it avoids parsing and processing overhead.createTemplate
) which can be slower to create than just assigning raw HTML to innerHTML
.insertAdjacentHTML
when dealing with large amounts of data, as it requires creating a new element and adding it to the DOM tree.insertAdjacentHTML
, but takes an individual HTML element instead of a fragment, making it faster to create.createElement
and inserting it into the DOM tree, which can be slower than just assigning raw HTML to innerHTML
.Library Usage
The test case uses the createTemplate
method from Web Components (WC), which is used to create a fragment of HTML that can be inserted into an existing element. The createTemplate
method creates a new Template
object, which is then used to insert the HTML fragment using insertAdjacentHTML
.
Special JS Features/Syntax
None mentioned in this benchmark.
Other Alternatives
If you need to optimize performance for adding or inserting HTML elements, consider the following alternatives:
Please note that these alternatives may have trade-offs and require additional setup or configuration.
The benchmark results suggest that insertAdjacentHTML
is the fastest approach, followed by insertAdjacentElement
. However, the actual performance difference between these methods can vary depending on the specific use case, browser version, and other factors.