Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Corrected DOM manipulation vs innerhtml
(version: 0)
Comparing performance of:
innerHTML vs DOM manipulation
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="root"></div>
Tests:
innerHTML
let root = document.querySelector('#root') id = '4/4b809ca8-b600-a286-62b6-83a6a5fc4310'; style = "left: 0px; top: 0px; white-space: nowrap;"; root.innerHTML = `<div id=${id} style=${style}>0</div>`; root.innerHTML = ''
DOM manipulation
let root = document.querySelector('#root') let itemBox = document.createElement('div') itemBox.id = "4/4b809ca8-b600-a286-62b6-83a6a5fc4310" itemBox.style = "left: 0px; top: 0px; white-space: nowrap;" itemBox.innerHTML = `0` root.appendChild(itemBox) root.removeChild(itemBox)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
innerHTML
DOM manipulation
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 benchmark and its components. **Benchmark Definition** The benchmark is defined by two test cases: `innerHTML` and `DOM manipulation`. The benchmark measures the performance difference between using `innerHTML` to set the content of an HTML element versus creating a new DOM node, manipulating its attributes, and appending it to the original element. **Options Compared** Two options are compared: 1. **`innerHTML`**: This method sets the inner HTML of an element by replacing all existing child nodes with new content. 2. **DOM manipulation**: This approach creates a new DOM node, sets its attributes (e.g., `id`, `style`), and appends it to the original element. **Pros and Cons** * **`innerHTML`**: + Pros: Simple, easy to use, and often faster since it can bypass some browser optimizations. + Cons: Can lead to performance issues if used excessively or with complex content, as it may not be optimized for frequent changes. * **DOM manipulation**: + Pros: Provides more control over the DOM tree and allows for better optimization of frequently changing elements. + Cons: More verbose and requires understanding of DOM node creation, attribute setting, and appending. In general, `innerHTML` is a good choice when you need to set simple content, while DOM manipulation is preferred when you require more complex manipulations or need fine-grained control over the DOM tree. **Library/Libraries** None are explicitly mentioned in this benchmark. However, it's worth noting that some browsers have optimized their inner HTML handling, which can lead to performance differences between `innerHTML` and DOM manipulation approaches. **Special JS Features/Syntax** The benchmark uses: * **Template literals**: Used in the `innerHTML` test case to set the content of the element. * **Template literals are not used in the "DOM manipulation" approach. Instead, string concatenation (`+`) is used to create the HTML string. Other than template literals and string concatenation, the benchmark code uses standard JavaScript features. **Alternatives** Other approaches to achieve similar results could be: 1. Using a DOM library like jQuery or a lightweight alternative. 2. Creating a virtual DOM and synchronously updating it when necessary. 3. Utilizing browser-specific APIs (e.g., WebAssembly, Canvas) for complex computations. However, these alternatives might not provide the same level of control over the DOM tree as DOM manipulation or `innerHTML` approaches.
Related benchmarks:
innerHTML vs removeChild (firstChild and lastChild)
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?