var simpleTemplate = document.createElement("template");
simpleTemplate.innerHTML = `<span class="simple"></span>`;
var complexTemplate = document.createElement("template");
complexTemplate.innerHTML =
`<span class="complex">
<span class="simple-1"></span>
<span class="simple-2">
<span class="simple-2-1"></span>
</span>
<span class="simple-3">
<span class="simple-3-1"></span>
</span>
<span class="simple-4"></span>
</span>`;
var simpleElement = document.createElement("span");
simpleElement.classList.add("simple");
var complexElement = document.createElement("span");
complexElement.classList.add("complex");
complexElement.innerHTML =
`<span class="simple-1"></span>
<span class="simple-2">
<span class="simple-2-1"></span>
</span>
<span class="simple-3">
<span class="simple-3-1"></span>
</span>
<span class="simple-4"></span>`;
let el = document.createElement("div");
let content = simpleTemplate.content.cloneNode(true);
el.appendChild(content);
let el = document.createElement("div");
let content = simpleElement.cloneNode(true);
el.appendChild(content);
let el = document.createElement("div");
let content = complexTemplate.content.cloneNode(true);
el.appendChild(content);
let el = document.createElement("div");
let content = complexElement.cloneNode(true);
el.appendChild(content);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Simple - Template | |
Simple - JavaScript | |
Complex - Template | |
Complex - JavaScript |
Test name | Executions per second |
---|---|
Simple - Template | 1062047.8 Ops/sec |
Simple - JavaScript | 1531631.2 Ops/sec |
Complex - Template | 343953.1 Ops/sec |
Complex - JavaScript | 388776.6 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 measures the performance of two approaches to create elements: using HTML templates (template
) versus creating elements directly in JavaScript (normal element
).
There are four individual test cases:
div
element from an HTML template.div
element by cloning a normal JavaScript element.div
element from an HTML template.div
element by cloning a normal JavaScript element.Template Approach
Using HTML templates allows for faster creation of elements, as it bypasses the DOM manipulation and serialization steps. This approach is particularly useful when creating complex structures with nested elements.
Pros:
Cons:
template
element) and parsing the HTML template string.JavaScript Approach
Cloning a normal JavaScript element involves serializing its contents, which can lead to slower execution times compared to using templates.
Pros:
Cons:
Other Considerations
The benchmark also considers factors such as browser version (Chrome 126) and platform (Desktop), which may affect the performance results. Additionally, the test cases are executed at different frequencies per second (ExecutionsPerSecond
values).
Library/Functionality Overview
In this benchmark, two libraries/functionalities are used:
cloneNode(true)
: This method creates a deep clone of an element and its contents.innerHTML
and classList.add()
: These properties allow for setting the inner HTML and adding classes to elements.Special JS Feature/Syntax
There is no special JavaScript feature or syntax used in this benchmark, apart from using the cloneNode(true)
method, which is a standard DOM API function.
Alternatives
If you're interested in exploring alternative approaches, consider:
Keep in mind that this benchmark is designed to measure specific performance aspects of creating elements using templates versus JavaScript cloning, so exploring alternative approaches might require adapting the benchmarking setup and scenarios.