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');
const headingText = document.createTextNode('Heading');
const para1Text = document.createTextNode('Paragraph 1');
const para2Text = document.createTextNode('Paragraph 2');
const buttonText = document.createTextNode('Button');
heading.appendChild(headingText);
para1.appendChild(para1Text);
para2.appendChild(para2Text);
button.appendChild(buttonText);
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);
// 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');
const headingText = document.createTextNode('Heading');
const para1Text = document.createTextNode('Paragraph 1');
const para2Text = document.createTextNode('Paragraph 2');
const buttonText = document.createTextNode('Button');
heading.appendChild(headingText);
para1.appendChild(para1Text);
para2.appendChild(para2Text);
button.appendChild(buttonText);
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 | |
createElement createTextNode |
Test name | Executions per second |
---|---|
template | 176237.4 Ops/sec |
DocumentFragment | 147640.6 Ops/sec |
createElement | 200067.5 Ops/sec |
createElement createTextNode | 216041.1 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark compares three approaches to render HTML templates:
<template>
): This approach uses the template
element to define a template with inner HTML.document.createDocumentFragment()
and appends elements to it before appending the entire fragment to the DOM.createElement
: This approach creates individual elements (e.g., div
, h1
, p
, button
) and sets their inner HTML using the innerText
property.Comparison
The benchmark measures the performance of each approach by cloning the template element or document fragment, appending it to the DOM, and counting the number of executions per second. The results are compared across different browsers and platforms.
Pros and Cons
<template>
):createElement
:Library Usage
None of the benchmark test cases use a specific JavaScript library.
Special JS Features/Syntax
The benchmark uses special syntax for creating text nodes (document.createTextNode()
) but does not leverage any modern JavaScript features like template literals
or class components
.
Other Alternatives
To improve performance, alternative approaches could consider:
Keep in mind that these alternatives might come with additional complexity, dependencies, or trade-offs.