Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
innerHTML vs. replaceChildren (iframe insertion) v3
(version: 0)
Comparing performance of:
innerHTML vs replaceChildren
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="adCont"> <p>TestLabel</p> <h2>TestHeadline</h2> </div> <script> const obj = { width: 300, height: 250, entryPoint: "./path/to/iframe.html", }; const adCont = document.querySelector(".adCont"); </script>
Tests:
innerHTML
adCont.innerHTML = `<iframe width="${obj.width}" height="${obj.height}" src="${obj.entryPoint}"></iframe>`;
replaceChildren
const ADiframe = document.createElement("iframe"); ADiframe.width = obj.width; ADiframe.height = obj.height; ADiframe.src = obj.entryPoint; adCont.replaceChildren(ADiframe);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
innerHTML
replaceChildren
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
innerHTML
2033.0 Ops/sec
replaceChildren
1968.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the JavaScript benchmark you provided. **Benchmark Overview** The benchmark compares two approaches for inserting an iframe into an HTML element: using `innerHTML` and using `replaceChildren`. The test case creates a simple HTML structure with a `<div>` element, sets up some variables, and then inserts an iframe into the `<div>` using either method. **Approach 1: Using `innerHTML`** The first test case uses `innerHTML` to insert the iframe: ```javascript adCont.innerHTML = `<iframe width="${obj.width}" height="${obj.height}" src="${obj.entryPoint}"></iframe>`; ``` This approach involves parsing and manipulating the HTML string to create a new iframe element, which is then inserted into the original element using its `innerHTML` property. **Pros:** * Simple and easy to implement * Works well for small to medium-sized datasets **Cons:** * Can be slower than other approaches due to the overhead of parsing and manipulating the HTML string * May not perform well with very large datasets or complex HTML structures **Approach 2: Using `replaceChildren`** The second test case uses `replaceChildren` to insert the iframe: ```javascript const ADiframe = document.createElement("iframe"); ADiframe.width = obj.width; ADiframe.height = obj.height; ADiframe.src = obj.entryPoint; adCont.replaceChildren(ADiframe); ``` This approach involves creating a new iframe element and then replacing the contents of the original element using `replaceChildren`. The new iframe element is inserted into the DOM, which can be faster than parsing and manipulating an HTML string. **Pros:** * Can be faster than other approaches due to the direct manipulation of elements * Works well with large datasets and complex HTML structures **Cons:** * May require more code to set up and use * Can be slower if not implemented correctly (e.g., if the iframe is not properly cleaned up) **Library Used** The benchmark uses the `document` object, which is a built-in JavaScript library that provides access to the DOM. The `createElement`, `querySelector`, and `replaceChildren` methods are all part of this library. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. However, it's worth noting that modern browsers have optimized performance for certain DOM operations, such as `replaceChildren`. Additionally, some older browsers may not support certain features or methods, so it's always a good idea to test for compatibility. **Alternative Approaches** Other approaches that could be considered include: * Using `appendChild` instead of `replaceChildren` * Creating the iframe using a template engine (e.g., Handlebars) * Using a library like jQuery to simplify DOM manipulation * Optimizing the HTML structure and content for better performance It's worth noting that the best approach will depend on the specific use case, performance requirements, and browser support. This benchmark provides a good starting point for understanding the relative performance of different approaches.
Related benchmarks:
createTextNode vs textContent vs innerText vs innerHTML
createTextNode vs innerHTML vs innerText
insertAdjacentHtml vs innerHTML - Replace
replaceChildren vs appendChild for empty container
insertAdjacentHtml vs createContextualFragment
Comments
Confirm delete:
Do you really want to delete benchmark?