<html>
<body>
<div id="container">
</div>
<body>
<html>
var str = `<div>${Math.random().toString().slice(2)}</div>`;
var container = document.getElementById("container");
var res = '';
var temp = document.createElement('template')
for(i=0; i<30000; i++){
res += str
}
temp.innerHTML = res;
var div = document.createElement('div')
container.appendChild(div);
div.innerHTML = res;
div.innerHTML = res;
const template = document.createRange().createContextualFragment(res);
div.replaceChildren(template);
const newDiv = div.cloneNode(false);
const template = document.createRange().createContextualFragment(res);
newDiv.appendChild(template);
div.parentNode.replaceChild(newDiv, div);
div = newDiv;
const newDiv = div.cloneNode(false);
newDiv.append(temp.content.cloneNode(true));
div.parentNode.replaceChild(newDiv, div);
div = newDiv;
div.innerHTML = "";
div.append(temp.content.cloneNode(true));
div.innerHTML = "";
const template = document.createRange().createContextualFragment(res);
div.appendChild(template);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerHTML | |
createContextualFragment - replaceChildren | |
createContextualFragment - replace whole div | |
cloneNode(true) - replace whole div | |
cloneNode(true) - clear with innerHTML | |
createContextualFragment - clear with innerHTML |
Test name | Executions per second |
---|---|
RDFYjolf | 1.0 Ops/sec |
RDFYjolf | 1.0 Ops/sec |
RDFYjolf | 1.0 Ops/sec |
RDFYjolf | 1.0 Ops/sec |
RDFYjolf | 1.0 Ops/sec |
RDFYjolf | 1.0 Ops/sec |
The benchmark provided is designed to assess the performance of various methods for manipulating the DOM, particularly in terms of appending and replacing nodes in the browser. The primary comparison made is between using innerHTML
and createContextualFragment
with additional options for how the DOM elements are manipulated.
Using innerHTML
div.innerHTML = res;
innerHTML
innerHTML
can cause loss of event handlers attached to the elements being replaced.Using createContextualFragment
with replaceChildren
const template = document.createRange().createContextualFragment(res); div.replaceChildren(template);
createContextualFragment - replaceChildren
innerHTML
.Using createContextualFragment
with whole div replacement
const newDiv = div.cloneNode(false); const template = document.createRange().createContextualFragment(res); newDiv.appendChild(template); div.parentNode.replaceChild(newDiv, div); div = newDiv;
createContextualFragment - replace whole div
Using cloneNode
with append
const newDiv = div.cloneNode(false); newDiv.append(temp.content.cloneNode(true)); div.parentNode.replaceChild(newDiv, div); div = newDiv;
cloneNode(true) - replace whole div
Using cloneNode
with clear
div.innerHTML = ""; div.append(temp.content.cloneNode(true));
cloneNode(true) - clear with innerHTML
innerHTML
for clearing the content while taking advantage of cloneNode
for adding new content.innerHTML
regarding the loss of even listeners.Using createContextualFragment
with clear
div.innerHTML = ""; const template = document.createRange().createContextualFragment(res); div.appendChild(template);
createContextualFragment - clear with innerHTML
createContextualFragment
, while initially clearing the element.innerHTML
for element clearing.According to the benchmark results, the fastest method was cloneNode(true) - replace whole div
, which executed at approximately 110.54 executions per second, followed by the innerHTML
method at approximately 56.97 executions per second. The slower methods included various implementations of createContextualFragment
, which suggests that while they offer advantages in managing DOM integrity and event handling, they can sometimes be less performant than simpler methods.
Other alternatives to DOM manipulation could include using:
In conclusion, effective DOM manipulation requires a careful consideration of trade-offs between performance, complexity, and maintaining existing event bindings or element data. Your choice may depend on the specific use case and performance requirements of the application.