<div id='t'></div>
function createExample() {
return document.createElement('div');
}
let fragment = document.createDocumentFragment();
const cloneDiv = createExample();
for(let i = 0; i < 100; ++i) {
fragment.appendChild(cloneDiv.cloneNode(false));
}
document.getElementById('t').appendChild(fragment);
let fragment = document.createDocumentFragment();
for(let i = 0; i < 100; ++i) {
fragment.appendChild(createExample());
}
document.getElementById('t').appendChild(fragment);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Clone node | |
Create element |
Test name | Executions per second |
---|---|
Clone node | 5892.7 Ops/sec |
Create element | 4527.5 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition
MeasureThat.net is comparing two approaches to clone an HTML element:
cloneNode()
method.DocumentFragment
object and appending cloned elements to it, then appending the fragment to the DOM.Script Preparation Code
The script preparation code defines a function createExample()
that returns a new HTML div
element:
function createExample() {
return document.createElement('div');
}
This function is used in both test cases to create new elements for cloning or appending to the fragment.
Html Preparation Code
The HTML preparation code creates an empty div
element with ID "t" and assigns it to a variable:
<div id='t'></div>
This HTML element will be used as a target for appending cloned or created elements.
Individual Test Cases
There are two test cases:
createExample()
function 100 times and appends each clone to the DocumentFragment
object, then appends the fragment to the DOM.let fragment = document.createDocumentFragment();
const cloneDiv = createExample();
for (let i = 0; i < 100; ++i) {
fragment.appendChild(cloneDiv.cloneNode(false));
}
document.getElementById('t').appendChild(fragment);
div
elements using the createExample()
function and appends each one to the DocumentFragment
object, then appends the fragment to the DOM.let fragment = document.createDocumentFragment();
for (let i = 0; i < 100; ++i) {
fragment.appendChild(createExample());
}
document.getElementById('t').appendChild(fragment);
Pros and Cons
Both approaches have their advantages:
DocumentFragment
object and appends cloned elements to it, then appends the fragment to the DOM. This approach is simpler and doesn't require cloning individual elements, but it may create unnecessary objects in memory.Other Considerations
The benchmark measures the execution time of each test case, which can help determine which approach is faster for specific use cases. Additionally, the benchmark uses a DocumentFragment
object to reduce the number of DOM mutations, which can improve performance by avoiding unnecessary reflows and repaints.
Library: DocumentFragment
A DocumentFragment
object is a lightweight container that allows you to group multiple elements together without creating a new element in the DOM. It's used to reduce the number of DOM mutations when appending elements to the DOM.
Special JS Feature: None mentioned
There are no special JavaScript features or syntax used in this benchmark definition.