var list = [],
n = 0;
while(true) {
n++;
list.push(document.createElement('div'));
if(n===100000)
break;
}
var list = [],
n = 0,
node = document.createElement('div');
while(true) {
n++;
list.push(node.cloneNode(true));
if(n===100000)
break;
}
var list = [],
n = 0,
node = document.createElement('div');
while(true) {
n++;
list.push(node.cloneNode(false));
if(n===100000)
break;
}
var list = [],
n = 0,
node = document.createElement('div');
while(true) {
n++;
list.push(node.cloneNode());
if(n===100000)
break;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode true | |
cloneNode false | |
cloneNode no params |
Test name | Executions per second |
---|---|
createElement | 17.0 Ops/sec |
cloneNode true | 27.0 Ops/sec |
cloneNode false | 27.4 Ops/sec |
cloneNode no params | 27.7 Ops/sec |
What is being tested?
MeasureThat.net is testing the performance of two approaches for creating new DOM elements: createElement
and cloneNode
. The benchmark simulates creating 100,000 new DOM elements using each approach.
Options compared
The options being compared are:
document.createElement('div')
.node.cloneNode()
without specifying any parameters.node.cloneNode(false)
. This approach uses a shallow clone, which only copies the element's attributes and not its child nodes.node.cloneNode(true)
. This approach uses a deep clone, which recursively copies all child nodes.Pros and Cons of each approach
cloneNode
with no parameters for small to medium-sized datasets.cloneNode
with no parameters due to its deeper cloning process.Library used
The benchmark uses the document.createElement()
and Element.prototype.cloneNode()
methods, which are built-in DOM API functions in modern browsers.
Special JS feature/syntax
None mentioned.