Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
DOM manipulation vs innerhtml
(version: 0)
Benchmark.js
Comparing performance of:
innerHTML vs DOM manipulation
Created:
6 years ago
by:
Guest
Go 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;">0</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;" itemBox.innerHTML = `0` 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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
innerHTML
348K/s
DOM manipulation
253K/s
View exact numbers
Test name
Executions per second
✓
innerHTML
347,551 Ops/sec
DOM manipulation
252,958 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The provided JSON represents a benchmark that compares two approaches for setting the content of an HTML element: using `innerHTML` and manual DOM manipulation (e.g., creating a new element, setting its attributes, and appending it to another element). **Options Compared** The benchmark tests two options: 1. **innerHTML**: This approach uses the `innerHTML` property of the `Element` interface to set the content of an HTML element. 2. **DOM Manipulation**: This approach creates a new element using `document.createElement()`, sets its attributes, and appends it to another element using `appendChild()`. **Pros and Cons** **innerHTML**: Pros: * Fast and concise * Easy to use for simple cases Cons: * Can be slow if the content is large or complex (because of JavaScript engine's DOM parsing overhead) * Can lead to security issues if not sanitized properly **DOM Manipulation**: Pros: * More control over the rendering process * Can avoid some of the security concerns associated with `innerHTML` Cons: * Requires more code and can be slower due to the overhead of creating and manipulating elements * May have performance implications depending on the browser's DOM implementation **Other Considerations** Both approaches have their trade-offs. If you need to set simple, static content, `innerHTML` might be a better choice. However, if you're working with dynamic content or require more control over the rendering process, manual DOM manipulation might be a better fit. **Library and Special JS Features** In this benchmark, no specific libraries are used. However, if you're interested in exploring library-specific optimizations, some JavaScript engines (like V8) have built-in optimizations for certain libraries (e.g., React or Angular). **Special JS Feature** There is one special feature being tested here: the `style` attribute on the created element. In modern browsers, this can affect rendering performance because it introduces a CSS parser step. **Alternatives** If you're looking for alternative approaches to benchmarking JavaScript performance, consider: 1. **Benchmarking frameworks**: Tools like Benchmark.js, Jest-Benchmark, or Cypress provide more comprehensive testing capabilities. 2. **WebAssembly benchmarks**: If you're interested in exploring the performance of WebAssembly (WASM) code, there are specialized benchmarking tools available. In summary, MeasureThat.net provides a simple yet effective way to compare JavaScript performance for specific use cases, like DOM manipulation and `innerHTML` usage.
Related benchmarks
innerhtml vs removechild(firstChild) vs removechild(lastChild) vs innerText
innerhtml vs removechild(firstChild) vs removechild(lastChild) vs innerText vs replaceChildren()
Comments
Confirm delete:
Do you really want to delete benchmark?