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>`;
let el = document.createElement("div");
let content = complexTemplate.content.cloneNode(true);
el.appendChild(content);
let el = document.createElement("span");
el.classList.add("complex");
let span1 = document.createElement("span");
span1.classList.add("simple-1");
let span2 = document.createElement("span");
span2.classList.add("simple-2");
let span21 = document.createElement("span");
span21.classList.add("simple-2-1");
span2.appendChild(span21);
let span3 = document.createElement("span");
span3.classList.add("simple-3");
let span31 = document.createElement("span");
span31.classList.add("simple-31");
span3.appendChild(span31);
let span4 = document.createElement("span");
span4.classList.add("simple-4");
el.append(span1, span2, span3, span4);
let el = document.createElement("div");
el.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>`;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
complex template | |
complex javascript | |
complex innerhtml |
Test name | Executions per second |
---|---|
complex template | 154220.8 Ops/sec |
complex javascript | 82695.2 Ops/sec |
complex innerhtml | 80530.9 Ops/sec |
Benchmark Overview
The provided benchmark, hosted on MeasureThat.net, compares the performance of three different approaches to create DOM nodes:
template
elementsOptions Compared
template
element introduced in ECMAScript 2015, which allows for efficient creation of complex DOM structures.document.createElement()
method and then appends them to a parent element.Pros and Cons
Library Usage
The benchmark uses the template
element, which is a built-in JavaScript feature introduced in ECMAScript 2015. This library has no external dependencies and only requires modern browsers that support the template
element.
Special JS Features or Syntax
This benchmark uses the template
element, which is a special JavaScript feature introduced in ECMAScript 2015. No other special features or syntax are used beyond what is required for the benchmark itself.
Alternatives
Other approaches to creating DOM nodes include:
These alternatives may offer additional benefits, such as improved performance, better code organization, or easier maintenance, but they often come with additional dependencies, learning curves, or performance overheads.