<ul id="test-ul">
</ul>
<div id="test-div">
</div>
var ul = document.getElementById('test-ul');
var docfrag = document.createDocumentFragment();
var browserList = ["Internet Explorer", "Firefox", "Safari",
"Chrome", "Opera"];
browserList.forEach(function(e) {
var li = document.createElement("li");
li.textContent = e;
docfrag.appendChild(li);
});
ul.appendChild(docfrag);
var div = document.getElementById('test-div');
var docfrag = document.createDocumentFragment();
var ul = docfrag.appendChild(document.createElement('ul'));
var browserList = ["Internet Explorer", "Firefox", "Safari",
"Chrome", "Opera"];
browserList.forEach(function(e) {
var li = document.createElement("li");
li.textContent = e;
ul.appendChild(li);
});
div.appendChild(docfrag);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Document Fragment 1 | |
Document Fragment 2 |
Test name | Executions per second |
---|---|
Document Fragment 1 | 54468.5 Ops/sec |
Document Fragment 2 | 46358.6 Ops/sec |
Overview of the Benchmark
The provided benchmark measures the performance of creating and appending document fragments to different HTML elements in various web browsers.
What is being tested?
The test cases are designed to create a document fragment (a new, empty HTML element) using the document.createDocumentFragment()
method. The fragment is then appended to either an unordered list (<ul>
) or another div
element using the appendChild()
method. The performance of this operation is compared across different web browsers and platforms.
Options being compared
The benchmark compares two approaches:
<ul>
element directly within the fragment (as shown in Test Case 2) versus<div>
element using appendChild()
method within the fragment (as shown in Test Case 1).Pros and Cons of each approach
<ul>
element to be created for every append operation, which can lead to increased memory usage and slower performance.appendChild()
within the fragment)Library used
The benchmark uses the Web API, specifically the document.createDocumentFragment()
method, which is a part of the standard HTML5 specification.
Special JavaScript feature or syntax
None mentioned in this explanation. However, it's worth noting that modern browsers support many features and APIs, including those related to DOM manipulation and web performance optimization.
Other alternatives
Alternative approaches to create document fragments include:
DOMTraversals
API (e.g., traverse()
method) to traverse the DOM tree and build the fragment.These alternatives may offer better performance or more convenient API usage but are outside the scope of this specific benchmark.