const el = document.createElement('div');
el.innerText = 'asssssssssss'
document.documentElement.appendChild(el);
const fragment = document.createDocumentFragment();
const el = document.createElement('div');
el.innerText = 'asssssssssss'
fragment.appendChild(el);
document.documentElement.appendChild(fragment);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
inner | |
fragment |
Test name | Executions per second |
---|---|
inner | 400443.1 Ops/sec |
fragment | 254161.5 Ops/sec |
Let's break down what's happening in this benchmark.
What is being tested?
The provided JSON represents two individual test cases, each testing the performance of creating and appending elements to different DOM structures.
Options compared:
const el = document.createElement('div');
) and directly appending it to document.documentElement
using el.appendChild(el);
. This approach is straightforward but might incur additional overhead due to the extra node creation.const fragment = document.createDocumentFragment();
) and then appending an element to it (fragment.appendChild(el);
). The resulting fragment is then appended to document.documentElement
using document.documentElement.appendChild(fragment);
. This approach can be more efficient because the fragment acts as a temporary storage for nodes, reducing the number of individual node creations.Pros and Cons:
Library usage:
None of these test cases use any libraries beyond the standard JavaScript APIs provided by the DOM.
Special JS features or syntax:
None mentioned.
Now, let's consider other alternatives:
WebKitAnimationTimer
class for benchmarking, which can provide more accurate results.In the context of MeasureThat.net, the choice between direct append and fragment-based append is likely intended to test the performance differences between these two approaches. The benchmark result suggests that fragment-based append performs better on this particular hardware configuration (Chrome 92 on Windows).