var list = [],
n = 0;
while(true) {
n++;
list.push(document.createElement('div'));
list[list.length-1].appendChild(document.createElement('div'));
list[list.length-1].appendChild(document.createElement('div'));
if(n===100000)
break;
}
var list = [],
n = 0,
node = document.createElement('div');
node.appendChild(document.createElement('div'));
node.appendChild(document.createElement('div'));
while(true) {
n++;
list.push(node.cloneNode(true));
if(n===100000)
break;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode |
Test name | Executions per second |
---|---|
createElement | 4.1 Ops/sec |
cloneNode | 11.9 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares the performance of two approaches for creating new complex DOM elements: document.createElement
versus node.cloneNode
.
Options Compared
The two options being compared are:
document.createElement
function, which returns an empty element that can be customized and appended to other elements.<div>
element) using the node.cloneNode(true)
function, which returns a deep copy of the original node.Pros and Cons
cloneNode
because it requires parsing and serializing the element's attributes, styles, and child nodes.createElement
because it reuses the existing node's memory, reducing overhead.Library and Syntax
In this benchmark, no libraries are used beyond the standard JavaScript DOM APIs.
There is no special JavaScript feature or syntax being tested in this benchmark.
Other Alternatives
If you're interested in exploring alternative approaches to creating DOM elements, consider the following options:
/
outerHTML`: Create a new element by parsing and serializing HTML strings.and
XMLSerializer`: Can be used to parse and serialize XML documents, which can then be used to create DOM elements.Keep in mind that these alternatives may not always provide better performance or more efficient results than the createElement
and cloneNode
methods.