Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Parse vs InnerHTML
(version: 0)
Parse vs InnerHTML
Comparing performance of:
parse() vs innerHTML
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script> <div id="demo"></div
Script Preparation code:
var html = "" for(var i = 0; i++; i < 1000){ html += `<ol> <li>a</li> <li>b</li> </ol> <ol> <li>c</li> <li>d</li> </ol>`; } var demo = document.querySelector("#demo") var range = document.createRange(); var parse = range.createContextualFragment.bind(range);
Tests:
parse()
demo.appendChild(parse(html)); document.querySelector("#demo").innerHTML = ""
innerHTML
demo.innerHTML = html; document.querySelector("#demo").innerHTML = ""
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
parse()
innerHTML
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 dive into the provided JSON benchmark data. **Benchmark Description** The benchmark tests two different approaches to parse and append HTML content to an element: `innerHTML` and `createContextualFragment`. The test creates 1000 `<ol>` elements with 2 `<li>` elements each, and then appends this HTML content to a container element using either the `innerHTML` property or the `createContextualFragment` method. **Options Compared** The two options being compared are: 1. **`innerHTML`**: This approach uses the `innerHTML` property of the `demo` element to set its inner HTML. This property is used to set the text content and HTML structure of an element. 2. **`createContextualFragment`**: This approach uses the `createContextualFragment` method on a `Range` object created from the `demo` element, and then passes this fragment to the `appendChild` method. **Pros and Cons** * **`innerHTML`**: + Pros: Simple and straightforward. Most people are familiar with using `innerHTML` to set HTML content. + Cons: Can be slower due to DOM mutations, which can lead to performance issues in some browsers. * **`createContextualFragment`**: + Pros: Faster, as it avoids the overhead of creating a new DOM element and setting its innerHTML. This approach is more suitable for large amounts of HTML content. + Cons: Requires understanding of the `Range` object and `createContextualFragment` method, which might be less familiar to developers. **Library/ Framework Used** In this benchmark, jQuery is used in the HTML preparation code (`<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>`). However, it's not explicitly used in the test cases themselves. Instead, the `createContextualFragment` method is called on a `Range` object created from the `demo` element. **Special JS Feature/ Syntax** The benchmark uses the `createContextualFragment` method, which is a modern JavaScript feature introduced in ECMAScript 2019 (ES10). This method allows you to create a new DOM fragment that can be appended to an existing element without causing a full reflow of the layout. **Other Considerations** When comparing these two approaches, it's essential to consider the context and requirements of your specific use case. If you need to set HTML content quickly and don't mind using `innerHTML`, this approach might be sufficient. However, if you're working with large amounts of data or need to optimize performance, `createContextualFragment` might be a better choice. As for alternatives, some other approaches to append HTML content include: * Using the `DOMParser` API to parse the HTML string and create a new DOM element. * Creating a temporary DOM element using `document.createElement`, setting its innerHTML, and then appending it to the target element. * Using a library like React or Angular, which provide optimized ways to manage and update DOM content. It's worth noting that these alternatives might have different performance characteristics and trade-offs compared to the two options being compared in this benchmark.
Related benchmarks:
Parse vs InnerHTML
Parse vs InnerHTML vs jQuery
Parse vs InnerHTML vs jQuery
innerText vs innerHTML vs textContent
Comments
Confirm delete:
Do you really want to delete benchmark?