var root = document.createElement("div");
document.body.appendChild(root);
for (var i = 0; i < 1000; i++) {
var e = document.createElement("span");
e.innerText = i + " text";
root.appendChild(e);
}
var frag = document.createDocumentFragment();
var root = document.createElement("div");
for (var i = 0; i < 1000; i++) {
var e = document.createElement("span");
e.innerText = i + " text";
root.appendChild(e);
}
document.body.appendChild(frag);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
createDocumentFragment |
Test name | Executions per second |
---|---|
createElement | 474.2 Ops/sec |
createDocumentFragment | 1003.7 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark on the MeasureThat.net website, which compares the performance of two approaches: document.createElement
and document.createDocumentFragment
. The benchmark is designed to measure the creation and appending of elements to a document.
Benchmark Definition
The benchmark definition consists of two test cases:
<div>
) using document.createElement
, appends it to the document body, and then creates 1000 <span>
elements with varying text content, each appended to the original element.document.createDocumentFragment
instance, which is not visible in the DOM, and then creates an element (<div>
) using document.createElement
, appends it to the fragment, and then creates 1000 <span>
elements with varying text content, each appended to the original element. Finally, the entire fragment is appended to the document body.Comparison of Options
The two approaches have different performance characteristics:
Library Usage
Neither of the test cases uses any external libraries. However, both approaches rely on the document
object, which is a part of the HTML DOM (Document Object Model).
Special JS Features/Syntax
The benchmark does not explicitly use any special JavaScript features or syntax. However, it assumes that the createDocumentFragment()
method is supported by the target browser, which may not be the case for older browsers.
Other Alternatives
There are other approaches to create elements and append them to the DOM, such as:
Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the document.createElement
and createDocumentFragment
approaches.