Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
innerhtml-vs-textcontent
(version: 1)
Comparing performance of:
textContent vs innerHTML
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id='ololoe'>Hello, World!</div>
Script Preparation code:
let el = document.querySelector("#ololoe");
Tests:
textContent
let i = 1000; let el = document.querySelector("#ololoe"); while (i--) { el.textContent = "Trololo!" }
innerHTML
let i = 1000; let el = document.querySelector("#ololoe"); while (i--) { el.innerHtml = "Trololo!" }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
textContent
innerHTML
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
textContent
2992.9 Ops/sec
innerHTML
359341.7 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON is designed to compare the performance of two different methods for updating the content of a DOM element in JavaScript: `textContent` and `innerHTML`. ### Options Compared: 1. **`textContent`**: - This property directly sets or retrieves the text content of a specified node and its descendants. It does not parse the text as HTML, meaning it treats the content as plain text, which helps avoid any security issues related to script injection from potentially unsafe HTML. - **Pros**: - More secure against XSS (Cross-Site Scripting) attacks, as it does not execute HTML or scripts within the text. - Usually faster since it doesn't require the browser to parse HTML, making it a better option for setting or updating plain text content. - **Cons**: - Limited to plain text—HTML tags are not rendered, meaning any HTML structure within the string is escaped and not displayed as HTML. 2. **`innerHTML`**: - This property sets or retrieves the HTML content (including text, elements, and attributes) of an element. When using `innerHTML`, you can insert complex markup, and it will render as such. - **Pros**: - Allows for more complex HTML structure to be created and rendered, facilitating dynamic UI updates where elements and formatting are needed. - **Cons**: - More vulnerable to XSS attacks if the content being assigned is not sanitized properly. - Generally slower than `textContent` because it requires parsing the string as HTML. ### Other Considerations: - **Performance**: As seen in the benchmark results, the `innerHTML` method managed 359,341 executions per second, while `textContent` only achieved approximately 2,993 executions per second. While these numbers demonstrate significant speed differences, the context of use is critical—if you're merely updating text without HTML need, `textContent` would be the preferred method despite its lower execution speed in this benchmark context. - **Use Case Evaluation**: The choice between `textContent` and `innerHTML` should depend largely on the specific needs of the application: - For cases where only text is manipulated, `textContent` is better suited because of its performance and security benefits. - When dealing with HTML content, `innerHTML` is necessary, but developers must ensure that the data going into it is properly sanitized to mitigate security risks. ### Alternatives: 1. **DOM Manipulation**: - Instead of using `innerHTML` for inserting multiple elements, you can create text nodes or element nodes using DOM methods like `document.createElement()` and `appendChild()`. This method also allows for better structure and element separation without risking security issues. 2. **Template Literals with HTML**: - Utilizing JavaScript template literals alongside the `insertAdjacentHTML()` method can be another alternative, especially for inserting markup without fully replacing existing content. 3. **Libraries/Frameworks**: - Many modern JavaScript frameworks (like React, Vue, and Angular) abstract DOM manipulation and may handle these methods internally while optimizing performance and security. These libraries usually manage updates more efficiently through virtual DOM techniques or similar strategies. By understanding these differences and considerations, software engineers can better determine which method to use for their specific circumstances in web development.
Related benchmarks:
innerHTML vs innerText vs textContent
innerText vs textContent vs innerHTML
setting innerHTML vs innerText vs textContent
innerHTML vs innerText vs custom
innerHTML vs innerText vs textContent 3
innerHTML vs innerText vs textContent Setting
innerHTML vs innerText vs textContent ..
innerHTML vs textContent
innerHTML vs innerText vs textContent with lower i
Comments
Confirm delete:
Do you really want to delete benchmark?