<div class='container'>
<div>
<a href="#">link</a>
</div>
</div>
<div id="appendedItems"></div>
var itemsContainer = document.getElementById('appendedItems');
for (i=0; i < 10000; i++) {
var fragment = document.createDocumentFragment();
var container = document.createElement('div');
container.className = 'container';
var div = document.createElement('div');
var a = document.createElement('a');
var aTextNode = document.createTextNode('link');
a.href = '#';
a.appendChild(aTextNode);
div.appendChild(a);
container.appendChild(div);
fragment.appendChild(container);
itemsContainer.appendChild(fragment);
}
var itemsContainer = document.getElementById('appendedItems');
var container = document.querySelector('.container');
for (i=0; i < 10000; i++) {
var fragment = document.createDocumentFragment();
var cloned = container.cloneNode(true);
var a = cloned.querySelector('a');
a.href = '##';
a.textContent = 'newLink';
fragment.appendChild(cloned);
itemsContainer.appendChild(fragment);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode |
Test name | Executions per second |
---|---|
createElement | 10.1 Ops/sec |
cloneNode | 11.4 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance of two approaches: creating an HTML structure using document.createElement
vs cloning an existing structure from the DOM using cloneNode
.
Script Preparation Code
The script preparation code is not provided, but it's likely that it includes a setup function that creates a test environment and sets up any necessary variables.
Html Preparation Code
The HTML preparation code is:
<div class='container'>
<div>
<a href=\"#\">link</a>
</div>
</div>
<div id="appendedItems"></div>
This code creates two nested <div>
elements, which will be used as the base structure for creating new HTML elements.
Individual Test Cases
There are two test cases:
document.createElement
to create a new HTML structure.<div>
element with a class of "container", another <div>
element inside it, and an <a>
element within the inner <div>
.document.createDocumentFragment
object and appends that fragment to the #appendedItems
container.cloneNode
to clone an existing HTML structure from the DOM.document.querySelector
.<a>
element).document.createDocumentFragment
object and appended to the #appendedItems
container.Library: Document Fragment
The document.createDocumentFragment
object is used in both test cases. This object allows us to create a new, empty HTML structure that can be appended to other elements without actually creating new DOM nodes. This can improve performance by reducing the number of DOM node creations.
Special JS Feature/Syntax
There are no special JavaScript features or syntaxes mentioned in this benchmark. However, it's worth noting that using document.createDocumentFragment
is a common pattern in JavaScript testing and benchmarking, as it allows for efficient manipulation of HTML structures without creating new DOM nodes.
Pros and Cons of Approaches
Other Considerations
When benchmarking JavaScript performance, it's essential to consider factors beyond just the approach used. Some other considerations include:
Alternatives
If you're looking for alternative approaches to benchmarking JavaScript performance, consider: