function getTemplateInnerHTML()
{
const template = document.createElement('template');
template.innerHTML = `
<div>
<h1>Heading</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<button>Button</button>
</div>
`;
const element = template.content.firstElementChild;
const clone = element.cloneNode();
document.documentElement.appendChild(clone);
return clone;
};
function getTemplateNodes()
{
const template = document.createElement('template');
const element = 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';
element.appendChild(heading);
element.appendChild(para1);
element.appendChild(para2);
element.appendChild(button);
template.content.appendChild(element);
document.documentElement.appendChild(element);
return element;
};
function getDocumentFragment()
{
const fragment = document.createElement('template');
const element = 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';
element.appendChild(heading);
element.appendChild(para1);
element.appendChild(para2);
element.appendChild(button);
fragment.appendChild(element);
document.documentElement.appendChild(fragment);
return fragment;
};
var templateInnerHTML = getTemplateInnerHTML();
var templateNodes = getTemplateNodes();
var documentFragment = getDocumentFragment();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Template with innerHTML | |
Template with Nodes | |
Document Fragment |
Test name | Executions per second |
---|---|
Template with innerHTML | 127980.9 Ops/sec |
Template with Nodes | 185356.6 Ops/sec |
Document Fragment | 290640.5 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Overview
The benchmark measures the performance of three different approaches to render HTML content:
template
element, clones its child nodes, and appends it to the document body.innerHTML
property to set the HTML content of a newly created template element.Options Being Compared
The benchmark compares the performance of these three approaches:
innerHTML
.Pros and Cons of Each Approach
Library and Purpose
The template
element is a built-in HTML element that allows for dynamic rendering of HTML content. In this benchmark, it's used to create a new template element, which serves as a container for the cloned or manually created DOM elements.
Special JS Feature/Syntax
There are no special JavaScript features or syntax being used in this benchmark. The code is standard JavaScript, using modern DOM manipulation techniques.
Other Alternatives
In general, other approaches to rendering HTML content might include:
However, these alternatives are not directly relevant to the specific benchmark being discussed.