const list = [];
let n = 0;
const div = document.createElement('div');
const div2 = document.createElement('div');
const div3 = document.createElement('div');
const p = document.createElement('p');
div2.classList.add('two');
div3.classList.add('three');
p.classList.add('font-bold');
p.textContent = 'Hello!';
div.appendChild(div2);
div2.appendChild(div3);
div3.appendChild(p);
console.log(div);
while(true) {
n++;
list.push(div);
if(n===100000)
break;
}
const list = [];
let n = 0;
const div = document.createElement('div');
const div2 = document.createElement('div');
const div3 = document.createElement('div');
const p = document.createElement('p');
div2.classList.add('two');
div3.classList.add('three');
p.classList.add('font-bold');
p.textContent = 'Hello!';
div.appendChild(div2);
div2.appendChild(div3);
div3.appendChild(p);
while(true) {
n++;
list.push(div.cloneNode(true));
if(n===100000)
break;
}
const list = [];
let n = 0;
const div = document.createElement('div');
div.innerHTML = '<div><div class="two3"><div class="three"><p class="font-bold">Hello!</p></div></div></div>'
console.log(div);
while(true) {
n++;
list.push(div);
if(n===100000)
break;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode (deep) | |
innerHTML |
Test name | Executions per second |
---|---|
createElement | 1001.0 Ops/sec |
cloneNode (deep) | 4.7 Ops/sec |
innerHTML | 1084.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Definition
The provided JSON represents a benchmark named "createElement vs cloneNode vs innerHTML for multi elements with optimized loops". This benchmark aims to measure which approach is faster when creating multiple DOM elements and appending them to an existing element.
Options Compared
There are three options being compared:
createElement
: Creates new DOM elements using the document.createElement()
method.cloneNode (deep)
: Clones existing DOM elements using the cloneNode(true)
method, which creates a deep clone of the element.innerHTML
: Sets the inner HTML of an element using the innerHTML
property.Pros and Cons
innerHTML
: This approach is fast but may not work well with complex elements or attributes.Library and Purpose
None are explicitly mentioned in this benchmark.
Special JS Feature or Syntax
There are no special JavaScript features or syntax used in this benchmark. It only relies on standard DOM APIs and JavaScript basics.
Other Alternatives
If you want to experiment with alternative approaches, here are some options:
DocumentFragment
instead of innerHTML
However, these alternatives may not be relevant to this specific benchmark, and the focus is on comparing the three main approaches: createElement
, cloneNode (deep)
, and innerHTML
.
Benchmark Preparation Code
The preparation code for this benchmark seems to be minimal, as it only creates some constants and an array. The actual benchmarking happens in the JavaScript code that defines each test case.
Keep in mind that creating a realistic and representative benchmark often requires more setup and preparation. This benchmark might be oversimplified or not representative of real-world scenarios.