box1 = document.createElement("div");
box2 = document.createElement("div");
box1.insertAdjacentHTML('beforeend', box2);
box1.appendChild(box2);
box1.insertAdjacentElement('beforeend', box2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
2) insertAdjacentHTML | |
3) appendChild | |
4) insertAdjacentElement |
Test name | Executions per second |
---|---|
2) insertAdjacentHTML | 136518.9 Ops/sec |
3) appendChild | 573261.4 Ops/sec |
4) insertAdjacentElement | 550334.2 Ops/sec |
Let's break down the benchmark definition and results.
Benchmark Definition:
The benchmark is called "innerHTML vs insertAdjacentHTML vs appendChild vs insertAdjacentElement 2". It compares four different methods for inserting HTML content into an existing HTML element:
insertAdjacentHTML
appendChild
insertAdjacentElement
The test case uses two HTML elements, box1
and box2
, which are created using the document.createElement()
method.
Test Cases:
There are three individual test cases:
insertAdjacentHTML
method to insert the content of box2
into box1
.box1.insertAdjacentHTML('beforeend', box2);
appendChild
method to append the content of box2
to box1
.box1.appendChild(box2);
insertAdjacentElement
method to insert the content of box2
into box1
, similar to insertAdjacentHTML
.box1.insertAdjacentElement('beforeend', box2);
Latest Benchmark Results:
The results show the execution speed ( Executions Per Second) of each test case on a Chrome 118 browser running on Windows:
What is being tested:
The benchmark is testing the performance of four different methods for inserting HTML content into an existing element:
insertAdjacentHTML
: Inserts HTML content as a string, without parsing it.appendChild
: Appends an existing HTML element to another element.insertAdjacentElement
: Inserts an existing HTML element into another element.Pros and Cons:
The choice of method depends on the specific use case:
insertAdjacentHTML
or insertAdjacentElement
, depending on the complexity of the appended element.insertAdjacentHTML
.Other Considerations:
When choosing an insertion method, consider factors like:
In this benchmark, the results show that appendChild
and insertAdjacentElement
have similar execution speeds on Chrome 118, while insertAdjacentHTML
is significantly slower. However, the choice of method should be based on the specific use case and requirements.