First of all, this is not the same as this question, although you may think it is as first glance: Does using a document fragment really improve performance?
See: this js fiddle https://jsperf.com/fragment-no-clone
If you are creating a bunch of new nodes in a loop with the intention of appending them to the DOM, in my MIND it is way faster to first create a fragment, append those elements to the fragment, and then append the fragment to the DOM after everything is set up. (as opposed to creating an element, append to DOM, create the next element, append to DOM, one after the other)
Here is a bit of code just to illustrate what I mean:
Using a Fragment
let fragment = document.createDocumentFragment();
for (var i = 0; i < 100; i++) {
let element = document.createElement('div');
fragment.appendChild(element);
}
document.body.appendChild(fragment);
Not using a Fragment
for (var i = 0; i < 100; i++) {
let element = document.createElement('div');
document.body.appendChild(element);
}
The reason why I think the fragment should be faster is because my understanding of how browsers work: If I'm just appending elements one after the other immediately then the browser has to do way more painting and calculating, whereas if I set it up nicely in a fragment and then append everything all at once, the browser only has to paint/calculate once
HOWEVER.
I am not really seeing that kind of performance gain in reality. Testing Chrome/Safari on that fiddle the difference is negligible, and while in Firefox there is a small boost in favour of using a fragment, it's nothing huge.
So what's happening under the hood? Is my understanding totally flawed?