<div id="container"></div>
var fragment = document.createDocumentFragment();
var text = 'Text node';
var el = document.createElement('span');
el.innerText = text;
fragment.appendChild(el);
const content = document.createTextNode(text)
fragment.appendChild(content)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerText | |
createTextNode |
Test name | Executions per second |
---|---|
innerText | 690784.6 Ops/sec |
createTextNode | 1226672.9 Ops/sec |
Let's dive into the provided benchmark.
What is being tested?
MeasureThat.net is testing which approach is faster for creating and appending text nodes to a documentFragment
in JavaScript. The test compares two methods: setting the innerText
property of an element directly, and creating a new Text
node using the document.createTextNode()
method.
Options compared:
<span>
element) directly.Text
node and appends it to the fragment
.Pros and Cons:
Library and purpose:
document.createDocumentFragment()
: This method creates a new, lightweight DOM node that can be used as a container for other elements. The purpose is to improve performance by reducing the number of nodes in the document tree.document.createTextNode(text)
: This method creates a new text node with the specified content.Special JS feature or syntax:
None mentioned in this benchmark. However, it's worth noting that some browsers may optimize certain DOM operations using features like CSSOM (CSS Object Model) and DOM APIs.
Other alternatives:
In addition to the two methods being tested, other approaches for creating text nodes include:
Text
node as a child of another element, like a <div>
or <span>
.Keep in mind that the best approach for your specific use case will depend on factors like performance requirements, code readability, and browser compatibility.