Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
createElement vs innerHTML
(version: 0)
createElement vs innerHTML
Comparing performance of:
createElement vs innerHTML
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="x"></div>
Tests:
createElement
let fragment = new DocumentFragment(); for (let i = 0; i < 100_000; i++) { fragment.appendChild(document.createElement("div")); } document.querySelector(".x").append(fragment);
innerHTML
let fragment = ""; for (let i = 0; i < 100_000; i++) { fragment += "<div></div>"; } document.querySelector(".x").innerHTML = fragment;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
createElement
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):
Let's dive into the benchmark and explore what's being tested. **Benchmark Overview** The benchmark is designed to compare two approaches for creating HTML elements in JavaScript: `createElement` and `innerHTML`. The goal is to determine which approach is faster, more efficient, or has better performance characteristics. **Options Compared** There are two main options compared: 1. **Creating HTML elements using `createElement`**: This method involves calling the `createElement()` function from the `Document` object to create a new HTML element. The created element can then be manipulated and appended to other elements. 2. **Creating HTML elements using `innerHTML`**: This method involves concatenating string literals with HTML tags to construct the desired element structure. Finally, this concatenated string is assigned to an element's `innerHTML` property. **Pros and Cons of Each Approach** 1. **createElement**: * Pros: + More flexible and forgiving, as it allows for dynamic creation of elements. + Can be used with various DOM manipulation techniques (e.g., appending, inserting, removing). * Cons: + May incur additional overhead due to the need to create a new element object and append it to another element. 2. **innerHTML**: * Pros: + Faster and more efficient, as it avoids the overhead of creating a new element object. * Cons: + Less flexible and less forgiving, as it requires precise concatenation of HTML tags and values. + Can lead to security vulnerabilities if not used carefully (e.g., injecting user-inputted data). **Library and Features Used** In this benchmark, the `DocumentFragment` object is used in one of the test cases. A `DocumentFragment` represents a container for multiple DOM nodes that are committed all at once when `appendChild()` or `innerHTML` is called on it. In this case, the fragment is created using `createElement()`, and its contents are appended to another element using `appendChild()`. This approach can be useful for improving performance when working with large amounts of HTML content. **Other Considerations** * The benchmark uses a fixed test case size (100,000 elements) to compare the performance of both approaches. In real-world scenarios, you might want to vary the test case size or use more realistic usage patterns. * The benchmark doesn't account for other factors that might affect performance, such as CSS style or layout calculations. * The results are based on a single browser version and platform combination (Chrome 93 on Windows Desktop). Testing across different browsers, platforms, and devices is essential to ensure the benchmark's accuracy. **Alternatives** Other alternatives for creating HTML elements include: 1. **StringTemplate**: A lightweight templating engine that allows you to define templates as strings and then render them using a `render()` method. 2. **DOM libraries like jQuery**: These libraries often provide optimized methods for creating and manipulating DOM elements, such as `$()`, `.append()`, or `.html()`. 3. **Rendered HTML snippets**: Using pre-built, rendered HTML snippets can eliminate the need for client-side rendering altogether. Keep in mind that each alternative has its trade-offs and use cases. The choice ultimately depends on your specific requirements, performance constraints, and development needs.
Related benchmarks:
createTextNode vs innerHTML vs innerText
createTextNode vs textContent vs innerText vs innerHTML (for reading)
createElement vs cloneNode vs innerHTML for multi elements
L0n1 createElement vs cloneNode vs innerHTML
createElement vs cloneNode vs innerHTML vs innerHTML(single innerHTML)
Comments
Confirm delete:
Do you really want to delete benchmark?