Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
innerHTML vs appendChild
(version: 0)
Comparing performance of:
innerHTML vs DocumentFragment
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
innerHTML
const el = document.createElement('div'); el.innerHTML = ` <h1>Heading</h1> <p>Paragraph 1</p> <p>Paragraph 2</p> <button>Button</button> `; document.documentElement.appendChild(el);
DocumentFragment
const el = document.createElement('div'); const heading = document.createElement('h1'); const para1 = document.createElement('p'); const para2 = document.createElement('p'); const button = document.createElement('button'); heading.innerText = 'Heading'; para1.innerText = 'Paragraph 1'; para2.innerText = 'Paragraph 2'; button.innerText = 'Button'; el.appendChild(heading); el.appendChild(para1); el.appendChild(para2); el.appendChild(button); document.documentElement.appendChild(el);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
innerHTML
DocumentFragment
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
27 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
innerHTML
95988.3 Ops/sec
DocumentFragment
11947.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents two JavaScript microbenchmarks: `innerHTML` and `DocumentFragment`. These benchmarks compare the performance of two approaches to insert HTML content into a document. **Options Compared** 1. **innerHTML**: This approach sets the innerHTML property of an element directly, which involves parsing the HTML string and appending it to the DOM tree. 2. **DocumentFragment**: This approach uses a DocumentFragment object to create a temporary container for the HTML content, which is then appended to the DOM tree. **Pros and Cons** 1. **innerHTML**: * Pros: Simple and straightforward implementation. * Cons: Parsing the HTML string can be slow, especially for complex or large strings. 2. **DocumentFragment**: * Pros: Can improve performance by avoiding unnecessary DOM mutations, as the fragment is only appended to the DOM tree once. * Cons: Requires creating an extra object (the DocumentFragment) and may have additional overhead due to its creation. **Other Considerations** * The benchmark focuses on the performance difference between these two approaches, which is relevant for applications that need to handle large amounts of HTML content or require optimal rendering performance. * However, in modern web development, using a virtual DOM (e.g., React's ReactDOM) can further optimize the rendering process by minimizing unnecessary DOM updates. **Library and Special JS Features** Neither of these benchmarks uses any external libraries or special JavaScript features. They are straightforward, vanilla JavaScript implementations that focus on the performance difference between two basic approaches to inserting HTML content into a document. **Other Alternatives** * **Virtual DOM**: Implementing a virtual DOM can further optimize rendering performance by minimizing unnecessary DOM updates. * **Patch-based Rendering**: This approach involves updating only the parts of the DOM tree that need changing, rather than rebuilding the entire tree.
Related benchmarks:
createTextNode vs textContent vs innerText vs innerHTML
createTextNode vs innerHTML vs innerText
createTextNode vs textContent vs innerText vs append
createTextNode vs textContent vs innerText vs innerHTML
appendChild vs append
Comments
Confirm delete:
Do you really want to delete benchmark?