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 |
---|---|
innerHTML | |
DocumentFragment |
Test name | Executions per second |
---|---|
innerHTML | 231748.5 Ops/sec |
DocumentFragment | 294668.4 Ops/sec |
Let's dive into the explanation of the provided benchmark.
What is being tested?
MeasureThat.net is testing the performance difference between two approaches: using innerHTML
and using a DocumentFragment
. In both cases, we're creating a HTML structure with multiple elements (a heading, two paragraphs, and a button) and appending it to the DOM.
Options compared:
There are two options being compared:
innerHTML
property of an element directly. The innerHTML is set to a multiline string containing the HTML structure.DocumentFragment
, adding the HTML structure elements to it, and then appending the fragment to the DOM.Pros and Cons:
DocumentFragment
Library usage:
None of the provided benchmark definitions explicitly use any libraries. However, it's worth noting that if you were to include a library like jQuery in one of your test cases, its overhead and complexity could impact performance.
Special JS features/syntax:
There is no special JavaScript feature or syntax being tested in this benchmark. The tests focus on the performance difference between using innerHTML
versus a DocumentFragment
.
Other alternatives:
If you're looking for alternative approaches to improve performance, consider:
In summary, the benchmark is testing the performance difference between using innerHTML
versus a DocumentFragment
. The results indicate that using a DocumentFragment
can lead to faster execution times. However, this approach requires more code and understanding of DOM manipulation, which may not be suitable for all use cases or skill levels.