var str = `<div>${Math.random().toString().slice(2)}</div>`;
var temp = document.createElement('template')
var res = '';
for(i=0; i<30000; i++){
res += str
}
temp.appendChild(document.createRange().createContextualFragment(res))
temp.innerHTML = res
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createContextualFragment | |
innerHTML |
Test name | Executions per second |
---|---|
createContextualFragment | 46.3 Ops/sec |
innerHTML | 51.4 Ops/sec |
The benchmark in question compares two different methods of inserting a large number of DOM elements into a webpage using JavaScript. Specifically, it evaluates the performance of DocumentFragment.createContextualFragment
versus the standard element.innerHTML
property for appending HTML content.
Using createContextualFragment
:
temp.appendChild(document.createRange().createContextualFragment(res))
createContextualFragment
Using innerHTML
:
temp.innerHTML = res
innerHTML
Pros:
createContextualFragment
may result in better performance in some scenarios, especially when dealing with dynamic or complex HTML structures. This method generates a DocumentFragment
, which does not trigger reflows or repaints until elements are added to the live DOM.Cons:
createContextualFragment
can be more complex for developers not familiar with the API and may require more boilerplate code to manage range creation and manipulation.Pros:
innerHTML
is straightforward and widely understood. Many developers are accustomed to this feature, making it easier to read and maintain.innerHTML
enables quick insertion of large snippets of HTML without the overhead of DOM manipulation, allowing developers to focus more on other parts of the application.Cons:
innerHTML
on an element can be less efficient, especially when larger sets of HTML are involved. It may cause the browser to re-parse and refocus, potentially leading to performance bottlenecks due to reflows or repaints.innerHTML
can open exposure to XSS vulnerabilities if user-generated content is inserted without proper sanitation.Choosing between these methods should depend on the specific context of use:
createContextualFragment
would be more appropriate.innerHTML
is a practical choice.Other than the methods tested, several additional alternatives exist for manipulating the DOM:
DocumentFragment: Directly using DocumentFragment
without createContextualFragment
can also be considered for batch DOM modifications, minimizing the number of reflows.
Element.append() / Element.prepend(): These methods allow for adding new nodes to the DOM with much cleaner syntax, supporting a broader range of content (Text, DOM nodes) without needing to parse HTML strings.
Virtual DOM Libraries: Utilizing frameworks like React or Vue.js leverages a Virtual DOM approach, effectively managing updates and optimizing performance behind the scenes while abstracting direct DOM manipulation.
Template Literals with DocumentFragment: Developers can create elements directly from template strings combined with DocumentFragment
for performance without dealing with the parsing overhead of innerHTML
.
In conclusion, the choice between these options fundamentally depends on the specific application requirements—speed, security, maintainability, and user experience considerations will guide developers in selecting the most appropriate method for DOM manipulation.