box1 = document.createElement("div");
box2 = document.createElement("div");
box2HTML = box2.innerHTML;
box1.innerHTML += box2HTML;
box1.insertAdjacentHTML('beforeend', box2HTML);
box1.appendChild(box2);
box1.insertAdjacentElement('beforeend', box2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1) innerHTML += | |
2) insertAdjacentHTML | |
3) appendChild | |
4) insertAdjacentElement |
Test name | Executions per second |
---|---|
1) innerHTML += | 8808815.0 Ops/sec |
2) insertAdjacentHTML | 3561247.0 Ops/sec |
3) appendChild | 1691205.1 Ops/sec |
4) insertAdjacentElement | 1682284.1 Ops/sec |
What is being tested?
The provided JSON benchmark measures the performance of different methods for inserting HTML content into an HTML element:
innerHTML
: setting the inner HTML of an elementinsertAdjacentHTML
: using the insertAdjacentHTML
method to insert HTML content before, after, or inside another elementappendChild
: appending a child node (in this case, a div
element) to another elementinsertAdjacentElement
: using the insertAdjacentElement
method to insert an element before, after, or inside another elementOptions compared
The benchmark compares the performance of these four options:
innerHTML +=
: concatenating HTML strings using the +
operatorinsertAdjacentHTML
: using the insertAdjacentHTML
method with the 'beforeend'
argument (inserting content before the current element)appendChild
: appending a child node to another elementinsertAdjacentElement
: using the insertAdjacentElement
method with the 'beforeend'
argument (inserting an element before the current element)Pros and Cons
Here's a brief summary of the pros and cons of each approach:
'beforeend'
argumentinsertAdjacentHTML
when using the 'beforeend'
argument.insertAdjacentHTML
, but uses an element instead of HTML content.Library usage
None of the benchmark tests use any external libraries, as all methods are native to JavaScript.
Special JS features or syntax
The benchmark doesn't use any special JavaScript features or syntax beyond what's available in modern browsers. If you're using a specific browser or version that supports newer features, you may see differences in performance.
Other alternatives
There are other alternatives for inserting HTML content into an element, such as:
textContent
property to set text contentdiv
element and appending it to another element using appendChild
However, these approaches may have different performance characteristics and use cases compared to the methods tested in this benchmark.