<html>
<body>
<div id="container">
</div>
<body>
<html>
var str = `<div>${Math.random().toString().slice(2)}</div>`;
var container = document.getElementById("container");
var temp = document.createElement('template')
var res = '';
for(i=0; i<30000; i++){
res += str
}
temp.innerHTML = res;
var div = document.createElement('div')
container.appendChild(div);
div.innerHTML = res;
while (div.firstChild) {
div.removeChild(div.firstChild);
}
div.append(temp.content.cloneNode(true));
div.innerHTML = res;
div.replaceChildren(temp.content.cloneNode(true));
const newDiv = div.cloneNode(false);
newDiv.append(temp.content.cloneNode(true));
div.parentNode.replaceChild(newDiv, div);
div = newDiv;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
cloneNode(true) - While -> Remove | |
innerHTML | |
cloneNode(true) - replaceChildren | |
cloneNode(true) - replace whole div |
Test name | Executions per second |
---|---|
cloneNode(true) - While -> Remove | 2.6 Ops/sec |
innerHTML | 9.4 Ops/sec |
cloneNode(true) - replaceChildren | 3.8 Ops/sec |
cloneNode(true) - replace whole div | 23.6 Ops/sec |
The benchmark defined in the provided JSON tests the performance of various approaches for manipulating the DOM (Document Object Model) in JavaScript, specifically comparing methods for inserting and replacing nodes.
cloneNode(true) - While -> Remove
while (div.firstChild) {
div.removeChild(div.firstChild);
}
div.append(temp.content.cloneNode(true));
div
, then appends a cloned node from a template
.innerHTML
div.innerHTML = res;
div
using innerHTML
, which replaces all existing content.cloneNode(true) - replaceChildren
div.replaceChildren(temp.content.cloneNode(true));
replaceChildren()
, which replaces the children of div
with a cloned template node.cloneNode(true) - replace whole div
const newDiv = div.cloneNode(false);
newDiv.append(temp.content.cloneNode(true));
div.parentNode.replaceChild(newDiv, div);
div = newDiv;
div
is created, and the entire old div
is replaced with the new one containing the cloned template.document.createElement
, append
, and replaceChild
, which are integral to DOM manipulation in JavaScript but do not involve any additional libraries. The template
element serves as a way to hold HTML content that can be instantiated in the DOM without being rendered immediately.Several alternatives exist for DOM manipulation in JavaScript:
Frameworks and Libraries: Using libraries such as React, Vue.js, or Angular can abstract away some of the low-level DOM manipulation. This can improve performance through virtual DOMs or reactive programming models, where changes are batched and managed more effectively.
DocumentFragment: Utilizing DocumentFragment
can be beneficial for performance because it allows multiple nodes to be added to the DOM in a single operation, avoiding multiple reflows or repaints.
Web Components: Custom elements made with Web Components encapsulate behavior and styling, which can provide modularity and potential performance benefits when manipulating the DOM.
When choosing between these DOM manipulation approaches, engineers should consider the context of their application:
This benchmark provides valuable insights into the performance trade-offs of various DOM manipulation methods, crucial for developing efficient, responsive web applications.