var str = `<div>${Math.random().toString().slice(2)}</div>`;
var range = document.createRange();
var temp = document.createElement('template')
var res = '';
for(i=0; i<30000; i++){
res += str
}
var t1 = new Date().now
var test1 = range.createContextualFragment(res)
temp.innerHTML = res
var test2 = temp.content
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createContextualFragment | |
InnerHtml |
Test name | Executions per second |
---|---|
createContextualFragment | 42.8 Ops/sec |
InnerHtml | 35.7 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is tested?
The provided JSON represents two benchmark test cases:
createContextualFragment
: Measures the performance of using Document.createRange().createContextualFragment()
to insert a large amount of HTML content.InnerHtml
: Measures the performance of setting innerHTML
on an element and then accessing its content
property.Options compared
In both test cases, we have two options:
a) Using createContextualFragment()
b) Using innerHTML
These are the fundamental approaches being compared in each benchmark. The choice between these two methods has implications for performance, code readability, and potential browser compatibility issues.
Pros and cons of each approach
createRange()
API.Document
API.Other considerations
createContextualFragment()
approach may not work in older browsers or those with limited support for the createRange()
API. On the other hand, using innerHTML
ensures wide browser compatibility.createContextualFragment()
can make code more readable and maintainable, as it provides a clear and explicit way to insert HTML content. In contrast, relying on innerHTML
might lead to less readable code, especially when dealing with complex fragment insertion scenarios.Library usage
In this benchmark, there is no library being used explicitly. However, the createRange()
API is part of the W3C's DOM Standard and is widely supported across modern browsers.
Special JS feature or syntax
There are no special JavaScript features or syntaxes being used in these benchmark test cases. The code is written in vanilla JavaScript and relies on standard DOM APIs.
Alternatives to MeasureThat.net
If you're interested in exploring similar benchmarking tools, consider the following alternatives:
Keep in mind that each of these alternatives has its own strengths and weaknesses, so it's essential to choose the tool that best fits your specific needs and requirements.