<div id="container"></div>
const container = document.getElementById('container');
container.innerHTML = '';
for (let i = 0; i < 1000; i++) {
const div = document.createElement('div');
const p = document.createElement('d');
p.classList.add('font-bold');
p.textContent = 'Hello'+i+'!';
div.appendChild(p);
container.appendChild(div);
}
/*cleanup*/
container.innerHTML = '';
const container = document.getElementById('container');
container.innerHTML = '';
for (let i = 0; i < 1000; i++) {
container.innerHTML += '<div><p class="font-bold">Hello'+i+'!</p></div>';
}
/*cleanup*/
container.innerHTML = '';
const container = document.getElementById('container');
container.innerHTML = '';
for (let i = 0; i < 1000; i++) {
container.append('<div><p class="font-bold">Hello'+i+'!</p></div>');
}
/*cleanup*/
container.innerHTML = '';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
innerHTML | |
innerHTML append |
Test name | Executions per second |
---|---|
createElement | 541.3 Ops/sec |
innerHTML | 1.6 Ops/sec |
innerHTML append | 2165.0 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, the different approaches, their pros and cons, and other considerations.
Benchmark Overview
The provided benchmark compares three ways to create DOM elements: createElement
, innerHTML
, and innerHTML append
. The goal is to measure which method is faster for creating a large number of elements.
Test Cases
There are three test cases:
createElement
method, then appends it to the container.innerHTML
property.append
method to add all elements at once.Library Used: None
There are no libraries used in these test cases.
Special JS Feature/Syntax: None
None of the test cases use any special JavaScript features or syntax. They only rely on standard DOM methods and properties.
Pros and Cons of Each Approach
innerHTML
but faster because it uses a single method call instead of setting and appending individually.append
method.Other Considerations
Alternatives
If you want to test other methods for creating DOM elements, such as:
You can create new benchmark definitions and test cases with these approaches.