Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
innerHTML vs createElement
(version: 0)
Comparing performance of:
createElement vs innerHTML
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
createElement
document.body.append( document.createElement("p"), document.createElement("p"), document.createElement("p"), document.createElement("p"), document.createElement("p") );
innerHTML
document.body.innerHTML = "<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p>";
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:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
createElement
264276.6 Ops/sec
innerHTML
151272.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared, and considered in this JavaScript microbenchmark. **Benchmark Definition** The benchmark definition represents two approaches to create multiple elements on the HTML page: 1. `createElement`: This approach uses the `document.createElement()` method to create individual elements (in this case, `p` tags) and appends them to the document body. 2. `innerHTML`: This approach sets the inner HTML of an existing element (in this case, the `body` element) using a string containing multiple `<p>` tags. **Comparison** The benchmark compares the performance of these two approaches: * **createElement**: Creating individual elements and appending them to the DOM. * **innerHTML**: Setting the inner HTML of an existing element using a string. **Pros and Cons** ### createElement Pros: * More explicit control over element creation and manipulation * Can be more efficient for dynamic or complex content creation * Allows for better error handling and debugging Cons: * Requires more memory allocation and garbage collection due to individual elements being created * May lead to slower performance if many elements need to be appended repeatedly ### innerHTML Pros: * Faster execution time since it involves setting the inner HTML of an existing element * Less memory allocation is required, as only a single string needs to be created Cons: * Less control over element creation and manipulation * Can lead to slower performance if the inner HTML changes frequently or is very large * May cause issues with accessibility and SEO if not implemented correctly **Library and Special JS Features** Neither of these approaches relies on specific libraries or JavaScript features. However, it's worth noting that using `innerHTML` can be affected by browser-specific features like: * ContentEditable API (not used in this benchmark) * HTMLImport (not used in this benchmark) **Other Alternatives** For more complex content creation, you might consider the following alternatives: 1. **Template literals**: Use template literals to create a string that can be parsed and rendered as HTML. 2. **DOM manipulation libraries**: Libraries like jQuery or DOM manipulators can provide an efficient way to manipulate the DOM. 3. **Server-side rendering**: Render content on the server using a templating engine, which can reduce the number of requests made by JavaScript. Keep in mind that these alternatives might introduce additional complexity and dependencies, so choose them carefully based on your specific use case and performance requirements.
Related benchmarks:
createTextNode vs textContent vs innerText
createTextNode vs textContent vs innerText vs innerHTML
createTextNode vs innerHTML vs innerText
createTextNode vs textContent vs innerText vs innerHTML
createTextNode vs textContent vs innerText vs innerHTML (for reading)
Comments
Confirm delete:
Do you really want to delete benchmark?