Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Parse vs InnerHTML
(version: 0)
Parse vs InnerHTML
Comparing performance of:
parse() vs innerHTML
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="demo"></div>
Script Preparation code:
var html = "" for(var i = 0; i++; i < 1000){ html += `<ol> <li>a</li> <li>b</li> </ol> <ol> <li>c</li> <li>d</li> </ol>`; } var demo = document.querySelector("#demo") var range = document.createRange(); var parse = range.createContextualFragment.bind(range);
Tests:
parse()
demo.append(parse(html)); document.querySelector("#demo").innerHTML = ""
innerHTML
demo.innerHTML = html; document.querySelector("#demo").innerHTML = ""
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
parse()
innerHTML
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
parse()
479376.6 Ops/sec
innerHTML
2991046.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to set HTML content in an element: using the `innerHTML` property versus creating a DOM context with `createContextualFragment`. The test case uses a loop to generate 1000 elements with nested `<ol>` tags, which is then used to demonstrate the performance difference between these two methods. **What's being tested?** The benchmark tests the following options: 1. **innerHTML**: This option sets the inner HTML of an element using `demo.innerHTML = html;`. The `innerHTML` property is a string that represents the HTML content of an element. 2. **parse() (createContextualFragment)**: This option creates a DOM context with `createContextualFragment` and then uses it to parse the generated HTML content into the `demo` element using `demo.append(parse(html));`. **Pros and Cons** 1. **innerHTML**: * Pros: + Simple and widely supported. + Fast, as it bypasses the DOM parser. * Cons: + Can lead to security issues if used with untrusted data (e.g., injecting malicious HTML). + May cause issues with HTML parsing and validation. 2. **parse() (createContextualFragment)**: * Pros: + More control over the parsing process, as it allows for custom parsing logic. + Can be more efficient if the input data is structured and well-formed. * Cons: + More complex to implement, especially when dealing with nested HTML structures. + May have slower performance compared to `innerHTML` due to additional overhead. **Library: document.createRange()** The `createRange()` function creates a new range object that represents the user interface for getting and setting text content in an element. The `createContextualFragment()` method is then used to create a DOM context from this range, which allows for more efficient parsing of the generated HTML content. **Special JS feature/syntax** There doesn't appear to be any special JavaScript features or syntax being tested in this benchmark. However, it's worth noting that modern browsers support various features like `createContextualFragment`, but their usage and performance may vary across different implementations. **Alternatives** Other alternatives for setting HTML content in an element include: 1. **DOMParser**: This API allows parsing HTML strings into DOM nodes. 2. **XMLHttpRequest**: This object can be used to parse and load HTML content from a server-side response. 3. **String.prototype.replace()**: Some browsers support using regular expressions with `String.prototype.replace()` to update the contents of an element. Keep in mind that each approach has its own trade-offs and use cases, so it's essential to choose the right method for your specific requirements.
Related benchmarks:
Parse vs InnerHTML
Parse vs InnerHTML vs jQuery
Parse vs InnerHTML vs jQuery
innerText vs innerHTML (performance 3)
Comments
Confirm delete:
Do you really want to delete benchmark?