<div id="target"></div>
const makeClassed = (tag) => (className, textContent) => {
const el = document.createElement(tag);
el.classList.add(className);
if (textContent !== undefined) {
el.textContent = textContent;
}
return el;
};
const makeSpan = makeClassed("span");
var makeDiv = makeClassed("div");
var target = document.getElementById("target")
var group = {
number: 42
}
const fragment = document.createRange().createContextualFragment(`
<div class="group-before">Группа ${group.number}</div>
<div class="group-toggle">-</div>
<div class="rows-container"></div>
`);
const toggle = fragment.querySelector('.group-toggle');
const rows = fragment.querySelector('.rows-container');
target.appendChild(fragment);
const groupBefore = makeDiv('group-before', `Группа ${group.number}`);
const groupToggle = makeDiv('group-toggle', '-');
const rows = makeDiv('rows-container');
target.appendChild(groupBefore);
target.appendChild(groupToggle);
target.appendChild(rows);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createContextualFragment | |
appendChild |
Test name | Executions per second |
---|---|
createContextualFragment | 3670.6 Ops/sec |
appendChild | 94958.3 Ops/sec |
Overview
The provided benchmark is designed to compare the performance of two approaches: creating a DOM fragment using document.createRange().createContextualFragment()
and appending elements individually using appendChild()
. The benchmark uses JavaScript and the Document Object Model (DOM) of the web.
What's being tested?
In the first test case, createContextualFragment
, a DOM fragment is created using the document.createRange().createContextualFragment()
method. This method allows creating a new DOM node that includes all elements from the specified range, while excluding the elements outside that range. In this benchmark, a group of elements is created within the fragment and then appended to the target element.
In the second test case, appendChild
, individual elements are created using JavaScript functions (makeClassed
) and then appended one by one to the target element.
Options compared
The two approaches have different pros and cons:
document.createRange().createContextualFragment()
:appendChild()
:Other considerations
When choosing between these approaches, consider the following factors:
appendChild()
might be sufficient, but may require additional optimization techniques (e.g., batch updates).appendChild()
is often easier to understand and maintain.Library usage
The benchmark uses the document
object and its methods (createRange()
, createContextualFragment()
, and appendChild()
). There are no external libraries or frameworks used in this benchmark.
Special JavaScript features or syntax
There are no special JavaScript features or syntax used in these benchmarks. The focus is on comparing two basic DOM manipulation techniques.
Alternatives
Other alternatives for creating DOM nodes and managing the Document Object Model include:
innerHTML
: Using innerHTML
to set the content of an element can be faster than creating individual elements, but it may not provide the same level of control over the DOM structure.createElement()
and chain methods: Creating elements using createElement()
and chaining methods (e.g., appendChild()
) can be a simple and effective approach for small to medium-sized datasets.However, these alternatives might not offer the same performance benefits as creating a contextual fragment or using batch updates with appendChild()
.