Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DOM manipulation vs innerhtml long - 1
(version: 0)
Comparing performance of:
innerHTML vs DOM manipulation
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="root"></div>
Tests:
innerHTML
let root = document.querySelector('#root') root.innerHTML = `<div id="4/4b809ca8-b600-a286-62b6-83a6a5fc4310" style="left: 0px; top: 0px; white-space: nowrap;"> <span class="testing">0</span> <span class="testing">1</span> <span class="testing">2</span> <span class="testing">3</span> <span class="testing">4</span> </div>`
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;" for (let i=0; i<5;i++) { let itemSpan = document.createElement('span') itemSpan.className = "testing"; itemSpan.innerText = i; itemBox.appendChild(itemSpan); } root.appendChild(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:
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
innerHTML
90990.6 Ops/sec
DOM manipulation
63245.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Benchmark Overview** The benchmark is comparing two approaches to manipulate the DOM (Document Object Model) in JavaScript: 1. **innerHTML**: This approach uses the `innerHTML` property to set the content of an HTML element. 2. **DOM manipulation**: This approach creates elements programmatically using `document.createElement()` and appends them to another element. **Options Compared** The benchmark is comparing these two approaches because they have different performance characteristics: * **innerHTML**: This method is often faster because it involves a single operation to set the content of an element, which can be optimized by the browser. * **DOM manipulation**: This method requires creating and appending multiple elements, which can lead to slower performance due to the overhead of DOM operations. **Pros and Cons** Here are some pros and cons of each approach: **innerHTML** Pros: * Often faster due to optimized single operation * Simpler code to write Cons: * Less control over the DOM tree structure * May lead to unnecessary reflows or repaints if the content changes frequently **DOM manipulation** Pros: * More control over the DOM tree structure * Can be more efficient for complex layouts or structures Cons: * Often slower due to multiple DOM operations * Requires more code to write and manage **Other Considerations** When using `innerHTML`, it's essential to consider the following: * Avoid using `innerHTML` with unescaped HTML content, as this can lead to security vulnerabilities. * Use a library like jQuery or a custom solution to parse and sanitize user-inputted HTML. When using DOM manipulation, keep in mind: * Be mindful of the number of elements created and appended, as this can impact performance. * Consider caching or reusing elements when possible to reduce overhead. **Library and Special JS Features** In this benchmark, no libraries are explicitly mentioned. However, it's worth noting that MeasureThat.net often uses a custom solution to parse and execute JavaScript code, which is not publicly disclosed. If the benchmark used special JavaScript features like async/await or Promises, they would be executed as part of the script preparation code. Since this is not the case here, we can ignore these aspects. **Other Alternatives** For comparing DOM manipulation approaches, other alternatives could include: * Using a template engine like Handlebars or Mustache * Creating elements using a library like React or Vue.js * Implementing a custom DOM builder When choosing an alternative, consider factors like performance, ease of use, and the specific requirements of your project. I hope this explanation helps software engineers understand what's being tested in this benchmark!
Related benchmarks:
getElementById vs id function
DOM manipulation vs innerhtml (corrected)
Corrected DOM manipulation vs innerhtml
querySelector vs. getElementsByClassName nested dom
querySelectorAll() vs getElementsByTagName() - with constant length
Comments
Confirm delete:
Do you really want to delete benchmark?