Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DOMParser vs createElement
(version: 0)
which is faster for converting raw html into textContent
Comparing performance of:
DOMParser() vs createElement
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
DOMParser()
let parser = new DOMParser(); let html = `<div id=":27" class="a3s aiL ">\[\begin{CD}<br> A @>a>> B \\<br> @VbVV @AAcA \\<br> C @= D<br> \end{CD}\]<div class="yj6qo"></div><div class="adL"><br> </div></div>` let doc = parser.parseFromString(html, 'text/html'); result = doc.documentElement.textContent;
createElement
let div = document.createElement('div'); let html = `<div id=":27" class="a3s aiL ">\[\begin{CD}<br> A @>a>> B \\<br> @VbVV @AAcA \\<br> C @= D<br> \end{CD}\]<div class="yj6qo"></div><div class="adL"><br> </div></div>` div.innerHTML = html; result = div.textContent
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
DOMParser()
createElement
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0
Browser/OS:
Firefox 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
DOMParser()
21308.0 Ops/sec
createElement
17101.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in this benchmark. **Benchmark Purpose:** The goal of this benchmark is to compare the performance of two approaches for converting raw HTML into textContent: 1. Using `DOMParser`. 2. Creating a new element using `document.createElement`, setting its innerHTML, and then extracting the textContent. **Options Compared:** * `DOMParser` vs `createElement` * Performance differences between these two approaches **Pros and Cons of Each Approach:** * **DOMParser:** + Pros: - More straightforward and concise way to parse HTML. - Less CPU-intensive compared to creating an element and setting its innerHTML. + Cons: - May not be suitable for all use cases (e.g., parsing complex, malformed HTML). - Can lead to performance issues if the input HTML is large or complex. * **createElement:** + Pros: - More flexible and powerful than `DOMParser`. - Can handle more complex HTML structures. + Cons: - Requires more code and is generally slower than using `DOMParser`. **Library/Functionality Used:** None explicitly mentioned in the benchmark definition, but `document` is used to access various DOM functions. **Special JS Feature/Syntax:** No specific JavaScript features or syntax are being tested in this benchmark. The focus is on comparing two different approaches for parsing HTML. **Other Alternatives:** If you're looking for alternative methods for converting raw HTML into textContent, consider the following: 1. **innerHTML**: Similar to `createElement`, but sets the innerHTML of an existing element. 2. **textContent**: Some DOM elements have a `textContent` property that can be set directly from an HTML string. 3. **parseFromString**: Other browsers might use this method for parsing HTML strings, although it's less commonly used than `DOMParser`. 4. **XSLT or XML parsers**: For more complex HTML structures, you might consider using XSLT or XML parsers. Keep in mind that the choice of method depends on your specific use case and performance requirements.
Related benchmarks:
createTextNode vs cloneNode asdf
createElement vs cloneNode (not deep) vs innerHTML
L0n1 createElement vs cloneNode vs innerHTML
createElement vs cloneNode vs innerHTML vs innerHTML(single innerHTML)
.createElement() vs .createHTMLDocument() vs DOMParser() vs .appendChild()
Comments
Confirm delete:
Do you really want to delete benchmark?