<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
<div id="demo"></div
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);
demo.appendChild(parse(html));
document.querySelector("#demo").innerHTML = ""
demo.innerHTML = html;
document.querySelector("#demo").innerHTML = ""
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
parse() | |
innerHTML |
Test name | Executions per second |
---|---|
parse() | 213367.6 Ops/sec |
innerHTML | 658914.1 Ops/sec |
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:
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.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
:innerHTML
to set HTML content.createContextualFragment
: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:
DOMParser
API to parse the HTML string and create a new DOM element.document.createElement
, setting its innerHTML, and then appending it to the target element.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.