const list = [];
let n = 0;
while(true) {
n++;
const div = document.createElement('div');
const p = document.createElement('p');
p.classList.add('font-bold');
p.textContent = 'Hello!';
div.appendChild(p);
list.push(div);
if(n===100000)
break;
}
const list = [];
let n = 0;
const div = document.createElement('div');
const p = document.createElement('p');
p.classList.add('font-bold');
p.textContent = 'Hello!';
div.appendChild(p);
while(true) {
n++;
list.push(div.cloneNode(true));
if(n===100000)
break;
}
const list = [];
let n = 0;
while(true) {
n++;
const div = document.createElement('div');
div.innerHTML = '<p class="font-bold">Hello!</p>';
list.push(div);
if(n===100000)
break;
}
const list = [];
let n = 0;
const div = document.createElement('div');
let s= '';
while(true) {
n++;
s+='<p class="font-bold">Hello!</p>';
if(n===100000)
break;
}
div.innerHTML = s;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode (deep) | |
innerHTML | |
innerHTML single |
Test name | Executions per second |
---|---|
createElement | 3.4 Ops/sec |
cloneNode (deep) | 9.0 Ops/sec |
innerHTML | 1.9 Ops/sec |
innerHTML single | 6.5 Ops/sec |
What is being tested on MeasureThat.net?
MeasureThat.net is testing four different approaches for creating new DOM elements: createElement
, cloneNode
(both shallow and deep), and innerHTML
. The test cases are designed to measure the execution speed of each approach, with a focus on creating 100,000 identical HTML elements.
Options being compared:
document.createElement()
method.cloneNode(true)
method, which creates a deep copy of the element.Pros and Cons of each approach:
innerHTML
for large numbers of identical elements, as it avoids the overhead of setting inner HTML content.createElement
, as cloning an existing element involves copying its attributes and child nodes.Library usage
None of the test cases use any external libraries or frameworks. The cloneNode
method is a native JavaScript function.
Special JS features or syntax
The test cases do not explicitly mention any special JavaScript features or syntax. However, the use of let
and const
declarations for variable assignment is modern JavaScript syntax introduced in ECMAScript 2015 (ES6).
Other alternatives
While not tested on MeasureThat.net, other approaches could include:
These alternatives would require additional test cases and benchmarking to determine their performance relative to the approaches tested on MeasureThat.net.