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;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode |
Test name | Executions per second |
---|---|
createElement | 5.8 Ops/sec |
cloneNode | 5693.5 Ops/sec |
Let's break down the provided benchmark and its test cases.
Benchmark Overview
The benchmark, named "createElement vs cloneNode23", compares two approaches to create new DOM elements: document.createElement
(createElement) and cloneNode
. The goal is to determine which approach is faster for creating a large number of DOM elements before insertion.
Options Compared
The two options being compared are:
document.createElement
: This method creates a new DOM element from scratch, specifying the element type as an argument.cloneNode
: This method creates a deep copy of an existing node, including its children. Although not directly applicable to creating new elements, it can be used to create multiple clones of an existing element.Pros and Cons
document.createElement
:cloneNode
:createElement
.Library and Special JS Features
The provided benchmark code does not use any specific JavaScript libraries. However, the use of cloneNode
is relevant because of its ability to create deep copies of nodes.
Test Case Explanation
The two test cases are identical, but with slight differences:
while
loop to push multiple elements onto an array using document.createElement
.document.createElement
, then clones it repeatedly using cloneNode
.Benchmark Preparation Code and HTML Preparation Code
The benchmark preparation code is empty ("Script Preparation Code": null, "Html Preparation Code": null
), indicating that no specific setup or initialization is required for the tests. This is likely because the test cases are self-contained and focus on comparing the performance of createElement
and cloneNode
.
Other Alternatives
Alternative approaches to create new DOM elements could include:
documentFragment
: A fragment node that can be used to create multiple elements without affecting the normal document flow.div
element with an invisible child: Creating a div
element with an invisible child (e.g., an empty string or null) to serve as a container for other elements.However, these alternatives may not directly compare to the basic createElement
and cloneNode
approaches.