<template id="PERFORMANCE-DOM-TEMPLATE">
<span class="simple"></span>
</template>
let template = document.createElement("template");
template.innerHTML = `<span class="simple"></span>`;
let DOMTemplate = document.getElementById("PERFORMANCE-DOM-TEMPLATE");
customElements.define(
"performance-template",
class extends HTMLElement {
constructor() {
super()
.attachShadow({
mode: "open",
})
.append(template.content.cloneNode(true));
}
}
);
customElements.define(
"performance-innerhtml",
class extends HTMLElement {
constructor() {
super().attachShadow({
mode: "open",
}).innerHTML = `<span class="simple"></span>`;
}
}
);
customElements.define(
"performance-dom-template",
class extends HTMLElement {
constructor() {
super()
.attachShadow({
mode: "open",
})
.append(DOMtemplate.content.cloneNode(true));
}
}
);
customElements.define(
"performance-dom-template-getelement",
class extends HTMLElement {
constructor() {
super()
.attachShadow({
mode: "open",
})
.append(
document.getElementById("PERFORMANCE-DOM-TEMPLATE").content.cloneNode(true)
);
}
}
);
let el = document.createElement("performance-template");
document.body.append(el);
let el = document.createElement("performance-innerhtml");
document.body.append(el);
let el = document.createElement("performance-dom-template-getelement");
document.body.append(el);
let el = document.createElement("performance-dom-template");
document.body.append(el);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JavaScript Template | |
only innerHTML | |
DOM Template, repeated getElementById | |
DOM Template, single getElementById |
Test name | Executions per second |
---|---|
JavaScript Template | 181129.0 Ops/sec |
only innerHTML | 164380.6 Ops/sec |
DOM Template, repeated getElementById | 139473.8 Ops/sec |
DOM Template, single getElementById | 50658.8 Ops/sec |
Overview of the Benchmark
The provided benchmark is designed to compare the performance of three different approaches for rendering HTML content in JavaScript microbenchmarks:
template
element and clones its contents using cloneNode(true)
.Template-based approach
This approach is designed to demonstrate the efficiency of creating a template element once and reusing it multiple times, rather than creating a new element for each cloning operation.
Pros:
Cons:
innerHTML-based approach
This approach is simple and easy to implement, but it may have performance implications due to the overhead of setting innerHTML on an element.
Pros:
Cons:
Element-by-Element approach
This approach creates separate elements for each cloning operation, which can lead to increased DOM mutations and potential performance issues.
Pros:
Cons:
Library usage
The benchmark uses the customElements
API, which is a modern JavaScript API for defining custom HTML elements. The template
element is used as part of this API.
Pros:
Cons:
Special JS features
The benchmark does not explicitly use any special JavaScript features or syntax, such as ES6 modules, async/await, or Web Workers. However, it may benefit from additional optimizations that take these features into account.
Other alternatives
Alternative approaches for rendering HTML content in JavaScript microbenchmarks include:
Keep in mind that these alternative approaches may require additional setup, resources, and expertise to implement effectively.