<div id="container"></div>
const node = document.getElementById('container');
for(let i = 0; i < 10000; i++) {
node.appendChild(document.createElement('div'));
}
const node = document.getElementById('container');
const elems = [];
for(let i = 0; i < 10000; i++) {
elems.push(document.createElement('div'));
}
node.replaceChildren(elems);
const node = document.getElementById('container');
const elems = [];
for(let i = 0; i < 10000; i++) {
elems.push(document.createElement('div'));
}
while (node.firstChild) {
node.removeChild(node.lastChild);
}
for(let i = 0; i < 10000; i++) {
node.appendChild(elems[i]);
}
const fragment = document.createDocumentFragment();
const node = document.getElementById('container');
const elems = [];
for(let i = 0; i < 10000; i++) {
elems.push(document.createElement('div'));
}
for(let i = 0; i < 10000; i++) {
fragment.appendChild(elems[i]);
}
node.replaceChildren(fragment);
const fragment = document.createDocumentFragment();
const node = document.getElementById('container');
const elems = [];
for(let i = 0; i < 10000; i++) {
elems.push(document.createElement('div'));
}
for(let i = 0; i < 10000; i++) {
fragment.appendChild(elems[i]);
}
node.innerHTML = '';
node.appendChild(fragment);
const fragment = document.createDocumentFragment();
const node = document.getElementById('container');
const elems = [];
for(let i = 0; i < 10000; i++) {
elems.push(document.createElement('div'));
}
for(let i = 0; i < 10000; i++) {
fragment.appendChild(elems[i]);
}
while (node.firstChild) {
node.removeChild(node.lastChild);
}
node.appendChild(fragment);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
replaceChildren | |
while w/ appendChild | |
replaceChildren w/ fragments | |
innerHTML w/ fragments | |
while w/ fragments |
Test name | Executions per second |
---|---|
replaceChildren | 114.4 Ops/sec |
while w/ appendChild | 112.6 Ops/sec |
replaceChildren w/ fragments | 114.4 Ops/sec |
innerHTML w/ fragments | 142.4 Ops/sec |
while w/ fragments | 102.7 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Benchmark Purpose
The benchmark is designed to compare the performance of different approaches for replacing child nodes in a DOM element. The test cases aim to measure which method is fastest, most efficient, and potentially suitable for use in real-world web applications.
Test Cases
There are six individual test cases:
replaceChildren
method on the DOM element to replace its child nodes with a new array of elements.removeChild
and then appends new child nodes using appendChild
.innerHTML
property to set the inner HTML of the element, followed by appending a fragment containing the new child nodes.Library and Functions Used
document.getElementById
, Array.prototype.push
, and DOM Element.prototype.replaceChildren
are used.Special JS Features/Syntax
None mentioned or used in this benchmark. The focus is on comparing different DOM manipulation approaches.
Other Considerations
When choosing a replacement method for child nodes:
Alternative Approaches
Other possible approaches for replacing child nodes might include:
DOMTraversals
(e.g., traverse()
, childList
) for more efficient iteration over child nodes.Template Literals
or Function Expressions
to create and manipulate DOM elements.Keep in mind that the choice of approach depends on specific use cases, performance requirements, and compatibility considerations.