document.body.innerHTML = "";
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(root);
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 | 558.0 Ops/sec |
createDocumentFragment | 592.3 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark test case hosted on MeasureThat.net. The benchmark compares the performance of two approaches: creating an HTML element using document.createElement()
versus creating a document fragment using document.createDocumentFragment()
. This test aims to measure which approach is faster for appending 1000 small elements to a parent element.
Options Compared
Two options are being compared:
createElement()
: Creates a new HTML element (in this case, a <span>
element) and appends it to the root element.createDocumentFragment()
: Creates a document fragment and appends small elements to it, which is then appended to the parent element.Pros and Cons of Each Approach
createElement()
:
Pros:
Cons:
createDocumentFragment()
:
Pros:
Cons:
Other Considerations
createDocumentFragment()
may help reduce the impact of DOM mutation on layout and painting.Library Usage
In this benchmark, document
is used, which is a built-in object in JavaScript. There are no external libraries required.
Special JS Features/Syntax
None mentioned in the provided JSON.