var list = [];
let n = 0;
while (++n != 100_000) {
list.push(document.createElement("div"));
}
var list = [];
let n = 0;
const node = document.createElement("img");
node.style.width = "24px";
while (++n != 100_000) {
list.push(node.cloneNode());
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode |
Test name | Executions per second |
---|---|
createElement | 19.8 Ops/sec |
cloneNode | 14.3 Ops/sec |
I'll break down the benchmark test and its options for you.
Benchmark Test Overview
The test compares two approaches to create new DOM elements:
createElement
: Creating a new DOM element using the document.createElement()
method.cloneNode
: Cloning an existing DOM element using the cloneNode()
method, rather than creating a new one.What is tested?
In this test, we're interested in determining which approach is faster for creating a large number of new DOM elements (100,000 in this case) before insertion into the document. The benchmark aims to measure the performance difference between these two approaches.
Options Compared
There are only two options being compared:
createElement
: Creates a new DOM element from scratch.cloneNode
: Clones an existing DOM element to create a new one.Pros and Cons of each approach:
Library: None
There is no library being used in this benchmark test. The document
object and standard JavaScript methods (createElement
, cloneNode
) are being used to compare the performance of these two approaches.
Special JS feature or syntax: None
This benchmark doesn't rely on any special JavaScript features or syntax, so it's accessible to developers with varying levels of knowledge.
Other alternatives
If you were to add more options to this benchmark, some alternative approaches could be:
These alternatives would allow you to explore additional aspects of performance optimization, but they're not part of this specific benchmark test.