Using DocumentFramgment
allows us to attach DOM elements to each other without causing a browser reflow (i.e. work with offline DOM trees). A lot of libraries like jQuery use document fragments to improve performance.
The document fragment can have a complicated structure. For example, let's say it represents something like:
<div>
<span>
<a href='asd'>asd</a>
</span>
<span>
<a href='asd2'>asd2</a>
</span>
<div>
<div>
Hello World
</div>
</div>
</div>
or a document fragment that contains multiple children:
<h2>Title 1</h2>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<h2>Title 2</h2>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
Often, when we finish building the the fragment, we attach it to the main DOM tree.
How many reflows happen when we do this? Does it depend on the number of direct children of the document fragment?
Update:
I got a response from Addy Osmani who is on the Chrome team at Google:
Just one DOM reflow. PS: we're moving more towards referring to reflow as layout as it's basically an event triggering layout/repaint in the page.