const html = "<span class='eChat-Custom-group-member Right'>5 members</span>"
const template = document.createElement("template");
template.innerHTML = html.toString().trim();
document.body.appendChild(template.content.firstChild);
var frag = document.createDocumentFragment();
frag.innerHtml = html.toString().trim();
document.body.appendChild(frag);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
createDocumentFragment |
Test name | Executions per second |
---|---|
createElement | 260212.4 Ops/sec |
createDocumentFragment | 5163857.5 Ops/sec |
The benchmark presented compares two different methods for inserting HTML content into the DOM using JavaScript. Specifically, it evaluates the performance of using a <template>
element vs. a DocumentFragment
. Here are the details:
Name: template vs documentFragment
Purpose: To determine the performance difference between two approaches to DOM manipulation by measuring how quickly each method can insert content into the document.
Script Preparation Code: The code snippet prepares a string containing HTML:
const html = "<span class='eChat-Custom-group-member Right'>5 members</span>";
createElement (Using <template>
)
const template = document.createElement("template");
template.innerHTML = html.toString().trim();
document.body.appendChild(template.content.firstChild);
<template>
element, sets its inner HTML content to the provided string, and then appends the first child of the template's content to the body. The <template>
element is a mechanism to hold client-side content that is not rendered immediately when the page loads.createDocumentFragment (Using DocumentFragment
)
var frag = document.createDocumentFragment();
frag.innerHtml = html.toString().trim();
document.body.appendChild(frag);
DocumentFragment
, which is a lightweight container for DOM elements. The aim here is to insert the HTML content into the fragment first and then append it to the body. However, it should be noted that there is a typo in the code (innerHtml
should be innerHTML
), which would prevent it from running correctly.The benchmark results indicate the number of operations executed per second for each method:
<template>
DocumentFragment
in this test.DocumentFragment
DocumentFragment
could potentially outperform in certain scenarios. <template>
does.Browser Compatibility: Both <template>
and DocumentFragment
are well-supported in modern browsers, but it's crucial to keep in mind that their performance can vary across different environments and older browsers.
Use Cases: The choice between the two often depends on specific needs. For example, if you plan on reusing the content multiple times or dynamically changing it, <template>
might be more beneficial. In contrast, if you're constructing complex DOM structures in a single go, DocumentFragment
might suit the task better.
Other approaches to manipulating the DOM might include:
Directly modifying the innerHTML
of an element: This can be quicker but introduces risks of XSS vulnerabilities if not handled properly.
document.body.innerHTML += "<span>New Content</span>";
Frameworks and Libraries: Using libraries such as React, Vue.js, or jQuery that provide their abstractions over DOM manipulation can lead to more manageable and often more efficient code, although they come with their overhead.
Virtual DOM: Techniques employed in frameworks like React can optimize rendering even further but introduce complication in how updates are managed.
In conclusion, the benchmark effectively demonstrates the differences between using <template>
and DocumentFragment
for DOM manipulation, focusing on the performance implications and practical use cases for software engineers.