const DIV = document.createElement('div')
const SPN = document.createElement('span')
const BTN = document.createElement('button')
const INP = document.createElement('input')
const H2 = document.createElement('h2')
const H4 = document.createElement('h4')
const AX = document.createElement('a')
const UL = document.createElement('ul')
const LI = document.createElement('li')
let u0 = UL.cloneNode(true)
let l0 = LI.cloneNode(true)
let l1 = LI.cloneNode(true)
let l2 = LI.cloneNode(true)
u0.appendChild(l0)
u0.appendChild(l1)
u0.appendChild(l2)
for (let i = 0; i < 200; ++i) {
let ui = u0.cloneNode(true)
let uj = ui.firstElementChild;
let uk = uj.nextElementSibling;
let ul = uk.nextElementSibling;
}
for (let i = 0; i < 200; ++i) {
let u0 = UL.cloneNode(true)
let l0 = LI.cloneNode(true)
let l1 = LI.cloneNode(true)
let l2 = LI.cloneNode(true)
u0.appendChild(l0)
u0.appendChild(l1)
u0.appendChild(l2)
}
let u0 = UL.cloneNode(true)
let l0 = LI.cloneNode(true)
let l1 = LI.cloneNode(true)
let l2 = LI.cloneNode(true)
u0.appendChild(l0)
u0.appendChild(l1)
u0.appendChild(l2)
l0.dataset.f00='1';
l1.dataset.f00='2';
l2.dataset.f00='3';
for (let i = 0; i < 200; ++i) {
let ui = u0.cloneNode(true)
let uj = ui.querySelectorAll('[data-f00]');
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
batch clone | |
singular clone | |
querySelectorAll |
Test name | Executions per second |
---|---|
batch clone | 8531.8 Ops/sec |
singular clone | 7707.4 Ops/sec |
querySelectorAll | 8017.5 Ops/sec |
The benchmark defined in the JSON compares two different approaches for cloning DOM nodes in JavaScript: batch cloning and singular cloning. Let's break down what each of these approaches entails, their pros and cons, and possible alternatives.
Batch Clone
ul
element (an unordered list) is created, and three li
elements (list items) are appended to it. Then this list is cloned multiple times (200), which results in the entire structure being duplicated:let u0 = UL.cloneNode(true)
let l0 = LI.cloneNode(true)
let l1 = LI.cloneNode(true)
let l2 = LI.cloneNode(true)
u0.appendChild(l0)
u0.appendChild(l1)
u0.appendChild(l2)
for (let i = 0; i < 200; ++i) {
let ui = u0.cloneNode(true)
}
Singular Clone
ul
and li
elements are created within a loop for each iteration. Each iteration constructs the list structure from scratch:for (let i = 0; i < 200; ++i) {
let u0 = UL.cloneNode(true)
let l0 = LI.cloneNode(true)
let l1 = LI.cloneNode(true)
let l2 = LI.cloneNode(true)
u0.appendChild(l0)
u0.appendChild(l1)
u0.appendChild(l2)
}
The benchmark results indicate that the batch cloning method performs significantly better than singular cloning. Specifically, the batch clone achieved approximately 15,773 executions per second, whereas the singular clone managed about 6,644 executions per second. This means that creating one template and cloning it multiple times is a more efficient use of resources compared to recreating the template for each iteration.
Pros:
Cons:
Pros:
Cons:
Using Document Fragments: If creating multiple similar elements, you could append them to a DocumentFragment
first and then add this fragment to the DOM in one operation, enhancing performance over directly manipulating the DOM multiple times.
Object Pooling: For frameworks or applications where cloned elements are frequently created and destroyed, maintaining a pool of reusable objects can greatly enhance performance, reducing the need for constant creation and garbage collection.
Library Support:
In conclusion, this benchmark effectively highlights the significance of how cloning strategies impact performance in JavaScript. By preferring batch cloning methods over singular ones, developers can create applications that are more responsive and efficient without needing to overhaul their coding practices substantially.