Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Manual vs InnerHTML
(version: 1)
Comparing performance of:
Manual vs InnerHTML
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="root"></div>
Tests:
Manual
function ListingJS() { const container = document.createElement("div"); container.style.padding = "2em 0"; const flexContainer = document.createElement("div"); flexContainer.style.display = "flex"; container.appendChild(flexContainer); const img = document.createElement("img"); img.src = "https://images.reverb.com/image/upload/s--_nQV81De--/t_card-square/v1471621675/ld0pn6zfzqrbihsskxxi.jpg"; img.alt = "Neumann U 87 Ai Large Diaphragm Multipattern Condenser Microphone"; img.style.width = "96px"; img.style.height = "96px"; flexContainer.appendChild(img); const textContainer = document.createElement("div"); textContainer.style.paddingLeft = "2em"; flexContainer.appendChild(textContainer); const h2 = document.createElement("h2"); h2.style.marginTop = "10px"; h2.textContent = "Fujifilm GFX 50S Medium Format Mirrorless Camera"; textContainer.appendChild(h2); const pathLink = document.createElement("div"); pathLink.className = "pathlink"; textContainer.appendChild(pathLink); const spans = [["/cams", "Cameras"], ["/cameras", "Mirrorless"], ["/fuji", "Fujifilm"]]; spans.forEach(([href, text]) => { const a = document.createElement("a"); a.href = href; a.textContent = text; pathLink.appendChild(a); const span = document.createElement("span"); span.className = "slash"; span.textContent = "/"; pathLink.appendChild(span); }); const priceContainer = document.createElement("div"); priceContainer.style.marginLeft = "auto"; flexContainer.appendChild(priceContainer); const priceSpan = document.createElement("span"); priceSpan.style.fontSize = "12px"; priceSpan.textContent = "Price range"; priceContainer.appendChild(priceSpan); const h3 = document.createElement("h3"); h3.style.margin = "0"; h3.textContent = "$1,600 - $1,900"; priceContainer.appendChild(h3); } ListingJS()
InnerHTML
function ListingInner() { const container = document.createElement("div"); container.style.padding = "2em 0"; container.innerHTML= `<div style="display: flex;"> <img src="https://images.reverb.com/image/upload/s--_nQV81De--/t_card-square/v1471621675/ld0pn6zfzqrbihsskxxi.jpg" alt="Neumann U 87 Ai Large Diaphragm Multipattern Condenser Microphone" style="width: 96px;height: 96px;"> <div style="padding-left: 2em;"> <h2 style="margin-top:10px">Fujifilm GFX 50S Medium Format Mirrorless Camera</h2> <div class="pathlink"> <span><a href="/cams">Cameras</a><span class="slash">/</span><a href="/cameras">Mirrorless</a><span class="slash">/</span><a href="/fuji">Fujifilm</a></span> </div> </div> <div style="margin-left: auto;"> <span style="font-size:12px">Price range</span> <h3 style="margin:0">$1,600 - $1,900</h3> </div> </div>` } ListingInner()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Manual
InnerHTML
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance difference between two approaches: manually creating HTML content and using the `innerHTML` property to set HTML content. **Manual vs InnerHTML Approach** In the manual approach, each component of the HTML is created separately (e.g., `img`, `div`, `h2`, etc.) and then appended to the container. This approach requires more overhead due to the repeated creation and manipulation of elements. In contrast, the `innerHTML` property allows you to set the entire HTML content at once, without creating individual elements. However, this approach can be slower if the innerHTML is complex or contains many DOM nodes. **Pros and Cons** Manual Approach: Pros: * More predictable performance, as each element is created separately * Can be beneficial for certain use cases where precise control over the DOM is required Cons: * Higher overhead due to repeated creation and manipulation of elements * May require more CPU cycles for element creation and appending InnerHTML Approach: Pros: * Faster execution time, especially for complex HTML content * Reduces the number of DOM nodes created, which can improve performance Cons: * Less predictable performance, as the browser's rendering engine needs to process the innerHTML * Can be slower if the innerHTML is complex or contains many DOM nodes **Library and Special JS Features** In this benchmark, no libraries are explicitly used. However, it's worth noting that some browsers may use their own optimization techniques or libraries for HTML parsing and rendering. No special JavaScript features are mentioned in the provided code snippets. **Alternatives** Other approaches to creating HTML content include: 1. **DocumentFragment**: Instead of using `innerHTML`, you can create a `DocumentFragment` and append your HTML content to it, which can be faster than using `innerHTML`. 2. **Shadow DOM**: If you need to create complex, isolated DOM structures, consider using Shadow DOM. 3. **Template Literals**: For simple HTML templates, template literals (e.g., `${htmlContent}`) can provide a convenient way to include dynamic content. **Best Approach** The best approach depends on the specific use case and requirements of your project. If predictable performance and precise control over the DOM are essential, the manual approach might be a better choice. However, if you need to create complex HTML content quickly, the `innerHTML` approach or document fragments might be more suitable. Keep in mind that this benchmark focuses on creating individual elements versus using innerHTML. The results may vary depending on your specific project requirements and JavaScript environment.
Related benchmarks:
innerHTML vs removeChild
querySelectorAll vs getElementsByTagName with two different tag names
querySelectorAll() vs getElementsByTagName()
querySelectorAll() vs getElementsByTagName() - with constant
querySelectorAll() vs getElementsByTagName() - with constant length
Comments
Confirm delete:
Do you really want to delete benchmark?