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;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode | |
cloneNode(false) |
Test name | Executions per second |
---|---|
createElement | 16.2 Ops/sec |
cloneNode | 27.1 Ops/sec |
cloneNode(false) | 29.3 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of creating new DOM elements using three different approaches: createElement
, cloneNode(true)
, and cloneNode(false)
. The goal is to determine which approach is faster for creating new DOM elements before insertion.
Options Compared
Pros and Cons of Each Approach
createElement
for very simple elements.Library Usage
None of the benchmark cases use a specific library, but they do rely on the browser's DOM API for creating and manipulating elements.
Special JS Features or Syntax
This benchmark does not explicitly use any special JavaScript features or syntax. However, it does demonstrate the differences in performance between various approaches to creating DOM elements.
Alternative Approaches
createElement()
and then setting its attributes manually.document.createElement('div').innerHTML = '<p>Hello World!</p>'
.DOMParser
API to parse HTML strings into DOM elements.These alternative approaches may be worth considering in specific use cases, but they may also introduce additional overhead or complexity compared to using createElement()
, cloneNode(true)
, and cloneNode(false)
directly.