box1 = document.createElement("div");
box2 = document.createElement("div");
fragment = document.createDocumentFragment();
box2HTML = box2.innerHTML;
box1.innerHTML += box2HTML;
box1.insertAdjacentHTML('beforeend', box2HTML);
box1.appendChild(box2);
box1.insertAdjacentElement('beforeend', box2);
box1.appendChild(fragment);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1) innerHTML += | |
2) insertAdjacentHTML | |
3) appendChild | |
4) insertAdjacentElement | |
5) documentFragment |
Test name | Executions per second |
---|---|
1) innerHTML += | 5015941.5 Ops/sec |
2) insertAdjacentHTML | 2140664.5 Ops/sec |
3) appendChild | 2437496.8 Ops/sec |
4) insertAdjacentElement | 2204402.5 Ops/sec |
5) documentFragment | 15946960.0 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark compares the performance of different approaches to append or insert HTML content into an existing DOM element.
Tested Options
The following options are compared:
innerHTML +=
insertAdjacentHTML('beforeend', ...)
appendChild(...)
insertAdjacentElement('beforeend', ...)
documentFragment
objectEach option is tested on its own, and the benchmark measures the number of executions per second for each option.
Pros and Cons
Here's a brief analysis of the pros and cons of each approach:
innerHTML +=
, as it avoids creating a new string with the combined content. However, it requires creating a new HTML element and appending it to the DOM.innerHTML +=
.insertAdjacentHTML
, but it uses an existing element instead of creating a new one. This can be more efficient than appendChild
.appendChild
if the existing element is not needed.documentFragment
object allows for appending multiple elements without causing unnecessary DOM mutations.Library Usage
The benchmark uses the following libraries:
None, as all tests are standalone JavaScript code.
Special JS Features or Syntax
This benchmark does not use any special JavaScript features or syntax. It only focuses on the basic DOM manipulation APIs provided by the browser.
Other Alternatives
If you need to perform similar benchmarking exercises, you can consider using other benchmarking tools and frameworks, such as:
Keep in mind that each tool has its own strengths and weaknesses, and some may be more suitable for your specific use case.