Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create element performance benchmark
(version: 0)
Testing the performance of creating the elements using createElement() and appendChild() vs simply setting the innerHTML to a string
Comparing performance of:
innerHTML vs createElement. classList, textContent, appendChild
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class='benchmark'></div>
Tests:
innerHTML
let articleHTML = '<article class="bg-yellow-100"> <h1 class="bold">Hello World</h1><p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Atque alias ullam eaque voluptas ab delectus reiciendis adipisci placeat, vero laborum illum dolorem quia odit? Cumque ratione vel saepe quas veniam. </p> </article>'; document.querySelector('.benchmark').innerHTML = articleHTML;
createElement. classList, textContent, appendChild
let article = document.createElement('article'); article.classList.add('bg-yellow-100'); let heading = document.createElement('h1'); heading.textContent = 'Hello World'; heading.classList.add('bold'); article.appendChild(heading); let paragraph = document.createElement('p'); paragraph.textContent = 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Atque alias ullam eaque voluptas ab delectus reiciendis adipisci placeat, vero laborum illum dolorem quia odit? Cumque ratione vel saepe quas veniam.'; article.appendChild(paragraph); document.querySelector('.benchmark').appendChild(article);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
innerHTML
createElement. classList, textContent, appendChild
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):
Let's break down the provided JSON and benchmark code to understand what is being tested. **Benchmark Overview** The benchmark compares the performance of two approaches for rendering HTML content: 1. **innerHTML**: Setting the innerHTML property of an element to render its content. 2. **createElement, classList, textContent, appendChild**: Creating elements using `document.createElement`, setting their class list and text content using `classList` and `textContent`, and appending them to another element using `appendChild`. **Comparison** The benchmark compares these two approaches because they have different performance characteristics: * **innerHTML**: + Faster execution time: Since it only requires a single DOM operation, setting the innerHTML is relatively fast. + Simpler implementation: It's easier for developers to implement using this method, as it avoids creating and manipulating elements manually. * **createElement, classList, textContent, appendChild**: + Slower execution time: This approach involves multiple DOM operations, which can be slower due to the overhead of element creation and manipulation. + More complex implementation: It requires understanding how to create and manipulate elements in a more explicit way. **Pros and Cons** * **innerHTML**: + Pros: - Faster execution time - Simpler implementation + Cons: - Less control over the DOM structure (elements are created implicitly) - May lead to performance issues if not optimized correctly * **createElement, classList, textContent, appendChild**: + Pros: - More control over the DOM structure (elements are created explicitly) - Can be more efficient for complex or dynamic content + Cons: - Slower execution time due to multiple DOM operations - Requires a deeper understanding of the DOM and element manipulation **Library Usage** The benchmark uses the **DOM API**, which is a set of APIs provided by web browsers for manipulating the Document Object Model (DOM). The DOM API allows developers to create, manipulate, and query elements in a web page. In this specific benchmark, the `document.createElement` method is used to create new elements, while the `classList` and `textContent` properties are used to set class lists and text content, respectively. The `appendChild` method is used to append child elements to another element. **Special JS Features** There are no special JavaScript features or syntax mentioned in this benchmark that would require a specific understanding of JavaScript beyond basic DOM manipulation concepts. **Alternatives** Other alternatives for rendering HTML content include: * **template literals**: Using template literals (e.g., `innerHTML`) can be faster and more convenient than traditional string concatenation. * **DOM-based rendering libraries**: Libraries like React or Angular provide their own solutions for rendering complex UI components, which may offer better performance and scalability. * **Server-side rendering**: In some cases, server-side rendering (SSR) can provide better performance and SEO benefits by rendering content on the server before sending it to the client. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and development goals.
Related benchmarks:
createElement vs cloneNode vs innerHTML
createElement vs cloneNode (not deep) vs innerHTML
L0n1 createElement vs cloneNode vs innerHTML
.createElement() vs .createHTMLDocument() vs DOMParser() vs .appendChild()
insertAdjacentHtml vs innerHTML (no initial contents)
Comments
Confirm delete:
Do you really want to delete benchmark?