const el = document.createElement('div');
el.innerHTML = `
<h1>Heading</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<button>Button</button>
`;
document.documentElement.appendChild(el);
const fragment = document.createDocumentFragment();
const el = document.createElement('div');
const heading = document.createElement('h1');
const para1 = document.createElement('p');
const para2 = document.createElement('p');
const button = document.createElement('button');
heading.innerText = 'Heading';
para1.innerText = 'Paragraph 1';
para2.innerText = 'Paragraph 2';
button.innerText = 'Button';
el.appendChild(heading);
el.appendChild(para1);
el.appendChild(para2);
el.appendChild(button);
fragment.appendChild(el);
document.documentElement.appendChild(fragment);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
inner | |
fragment |
Test name | Executions per second |
---|---|
inner | 112349.0 Ops/sec |
fragment | 143829.7 Ops/sec |
I'll break down the provided benchmark data for you.
Benchmark Definition JSON
The Benchmark Definition
JSON represents a JavaScript microbenchmark, which is a small piece of code designed to measure performance. In this case, there's only one benchmark definition:
{
"Name": "ssssdsaddffvfgg",
"Description": null,
"Script Preparation Code": null,
"Html Preparation Code": null
}
This JSON object is empty, which means that the script preparation code and HTML preparation code are not provided. This can be a deliberate design choice to allow users to customize their benchmarks or to keep the benchmarking process simple.
Individual Test Cases
There are two test cases:
{
"Benchmark Definition": "const el = document.createElement('div');\r\nel.innerHTML = `\r\n <h1>Heading</h1>\r\n <p>Paragraph 1</p>\r\n <p>Paragraph 2</p>\r\n <button>Button</button>\r\n`;\r\n\r\ndocument.documentElement.appendChild(el);",
"Test Name": "inner"
}
This test case creates a new <div>
element and sets its inner HTML using template literals. The document.documentElement.appendChild(el)
line appends the element to the document's root element.
{
"Benchmark Definition": "const fragment = document.createDocumentFragment();\r\n\r\nconst el = document.createElement('div');\r\nconst heading = document.createElement('h1');\r\nconst para1 = document.createElement('p');\r\nconst para2 = document.createElement('p');\r\nconst button = document.createElement('button');\r\n\r\nheading.innerText = 'Heading';\r\npara1.innerText = 'Paragraph 1';\r\npara2.innerText = 'Paragraph 2';\r\nbutton.innerText = 'Button';\r\n\r\nel.appendChild(heading);\r\nel.appendChild(para1);\r\nel.appendChild(para2);\r\nel.appendChild(button);\r\n\r\nfragment.appendChild(el);\r\ndocument.documentElement.appendChild(fragment);",
"Test Name": "fragment"
}
This test case creates a new <div>
element, three child elements (<h1>
, <p>
, and <button>
, each with its own text content), and then appends these elements to the fragment
object using appendChild
. Finally, it appends the fragment
to the document's root element.
Browser Results
The latest benchmark results show two browsers running the test cases:
{
"RawUAString": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36",
"Browser": "Chrome 92",
"DevicePlatform": "Desktop",
"OperatingSystem": "Windows",
"ExecutionsPerSecond": 143829.71875,
"TestName": "fragment"
}
{
"RawUAString": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36",
"Browser": "Chrome 92",
"DevicePlatform": "Desktop",
"OperatingSystem": "Windows",
"ExecutionsPerSecond": 112349.0390625,
"TestName": "inner"
}
Both browsers run the test cases on a desktop Windows platform, using Chrome 92.
Options Compared
The two test cases compare two approaches to set the inner HTML of a <div>
element:
**: sets the
innerHTML` property directly.**: creates a new
DocumentFragment`, appends child elements to it, and then appends the fragment to the document.Pros and Cons
DocumentFragment
object, which is optimized for appending child nodes..createDocumentFragment()
), which may incur additional overhead.Other Considerations:
"inner"
test case creates a temporary element during string interpolation. This might be optimized or ignored by some browsers."fragment"
approach uses appendChild
to add child elements to the fragment
object. This might lead to slower performance if the fragment grows large, as it may require additional DOM node creation and layout adjustments.Alternatives
Other alternatives could include:
Keep in mind that these alternatives would require significant changes to the benchmarking process, script preparation code, and execution setup.