const tpl = document.createElement('template');
tpl.innerHTML = `
<div>
<h1>Heading</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<button>Button</button>
</div>
`;
const el = tpl.content.firstElementChild
document.documentElement.appendChild(el.cloneNode());
const fragment = document.createDocumentFragment();
const el = document.createElement('div');
const heading = document.createElement('h1');
const para1 = document.createElement('p');
const para2 = document.createElement('p');
const button = document.createElement('button');
heading.innerText = 'Heading';
para1.innerText = 'Paragraph 1';
para2.innerText = 'Paragraph 2';
button.innerText = 'Button';
el.appendChild(heading);
el.appendChild(para1);
el.appendChild(para2);
el.appendChild(button);
fragment.appendChild(el);
document.documentElement.appendChild(fragment);
// const fragment = document.createDocumentFragment();
const el = document.createElement('div');
const heading = document.createElement('h1');
const para1 = document.createElement('p');
const para2 = document.createElement('p');
const button = document.createElement('button');
heading.innerText = 'Heading';
para1.innerText = 'Paragraph 1';
para2.innerText = 'Paragraph 2';
button.innerText = 'Button';
el.appendChild(heading);
el.appendChild(para1);
el.appendChild(para2);
el.appendChild(button);
// fragment.appendChild(el);
document.documentElement.appendChild(el);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
template | |
DocumentFragment | |
createElement |
Test name | Executions per second |
---|---|
template | 45910.2 Ops/sec |
DocumentFragment | 69776.0 Ops/sec |
createElement | 61045.4 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark compares three approaches to rendering HTML content in JavaScript:
template
element and its innerHTML
property.document.createDocumentFragment()
object to append child elements.Test Cases
Each test case represents a single benchmark, with the following components:
Let's analyze each test case:
const tpl = document.createElement('template');
tpl.innerHTML = '\r\n<div>\r\n <h1>Heading</h1>\r\n <p>Paragraph 1</p>\r\n <p>Paragraph 2</p>\r\n <button>Button</button>\r\n</div>\r\n';
const el = tpl.content.firstElementChild;
document.documentElement.appendChild(el.cloneNode());
This code creates a template
element, sets its innerHTML
, and then clones the resulting template to create an HTML document fragment. The cloned fragment is then appended to the document.documentElement
.
Pros and Cons:
const fragment = document.createDocumentFragment();
const el = document.createElement('div');
const heading = document.createElement('h1');
const para1 = document.createElement('p');
const para2 = document.createElement('p');
const button = document.createElement('button');
heading.innerText = 'Heading';
para1.innerText = 'Paragraph 1';
para2.innerText = 'Paragraph 2';
button.innerText = 'Button';
el.appendChild(heading);
el.appendChild(para1);
el.appendChild(para2);
el.appendChild(button);
fragment.appendChild(el);
document.documentElement.appendChild(fragment);
This code creates a DocumentFragment
object, sets up individual HTML elements, and appends them to the fragment. Finally, the fragment is appended to the document.documentElement
.
Pros and Cons:
const el = document.createElement('div');
const heading = document.createElement('h1');
const para1 = document.createElement('p');
const para2 = document.createElement('p');
const button = document.createElement('button');
heading.innerText = 'Heading';
para1.innerText = 'Paragraph 1';
para2.innerText = 'Paragraph 2';
button.innerText = 'Button';
el.appendChild(heading);
el.appendChild(para1);
el.appendChild(para2);
el.appendChild(button);
document.documentElement.appendChild(el);
This code creates individual HTML elements, sets their text content, and appends them to a div
element. The resulting element is then appended to the document.documentElement
.
Pros and Cons:
Alternative Approaches:
In summary, the benchmark compares three approaches to rendering HTML content in JavaScript:
template
element and its innerHTML
property.document.createDocumentFragment()
object to append child elements.Each approach has its pros and cons, and the choice of which one to use depends on the specific requirements of your project.