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 section = document.createElement('section');
section.innerHTML += `
<div>
<h1>Heading</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<button>Button</button>
</div>
`;
document.documentElement.appendChild(section);
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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
teste_template_string | |
teste_DocumentFragment |
Test name | Executions per second |
---|---|
teste_template_string | 31597.5 Ops/sec |
teste_DocumentFragment | 34787.0 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition and test cases are used to measure the performance of different approaches for creating HTML elements in JavaScript.
Script Preparation Code
The script preparation code creates an HTML template with a heading, two paragraphs, and a button. This template is then cloned and appended to the document's root element. There are two variations:
const tpl = ...
: Creates an HTML template using document.createElement('template')
. The template's innerHTML is set to the desired HTML content.const section = ...
: Creates a new <section>
element and appends its innerHTML directly to it.Html Preparation Code
The html preparation code is empty for both test cases, indicating that no additional HTML content needs to be created before running the benchmarks.
Test Cases
There are two individual test cases:
teste_template_string
: Creates an HTML template using const section = ...
, where the innerHTML of the <section>
element is set directly.teste_DocumentFragment
: Creates a new document fragment, creates four elements (<div>
, <h1>
, <p>
, and <button>
) inside it, sets their text content, and appends them to the fragment.Performance Comparison
The benchmark compares the performance of two approaches:
const tpl = ...
: Creates an HTML template using document.createElement('template')
.const section = ...
: Creates a new <section>
element and appends its innerHTML directly to it.Pros and Cons
const tpl = ...
):const section = ...
):Other Considerations
teste_DocumentFragment
test case uses a document fragment to optimize the creation of multiple elements. This approach can reduce DOM manipulation overhead and improve performance for large HTML documents.Alternatives
Some alternative approaches could be:
It's essential to note that the best approach depends on the specific use case, performance requirements, and browser support.