React.Fragment looks like DocumentFragment. But does React.Fragment has the same performance benefits like DocumentFragment?
In other words, are this
// index.jsx
const arr = [...Array.from({length: 10000})];
return (
<ul>
<>
{arr.map((x, i) => <li>{i}</li>)}
</>
</ul>
)
and this
// index.js
const arr = [...Array.from({length: 10000})];
const fragment = new DocumentFragment();
for(let i = 0; i < arr.length; ++i) {
const li = document.createElement('li');
li.textContent = i;
fragment.append(li);
}
document.querySelector('ul').append(fragment)
the same?
Or I still should use DocumentFragment for large data like below?
//index.jsx
const arr = [...Array.from({length: 10000})];
const fragment = new DocumentFragment();
for(let i = 0; i < arr.length; ++i) {
const li = document.createElement('li');
li.textContent = i;
fragment.append(li);
}
return (
<ul>
{fragment}
</ul>
)
Interesting question, I'm pretty sure
React.Fragmentis not implemented throughDocumentFragment, because my guts feeling there's no such need.React.Fragmentis simply added to make sure we can doBut in case we want to specify the element type, we can use
React.Fragment. I'm sure you already know what it does, it's a wrapper that allow you to wrap around a group of elements without adding the physical wrapper element to the DOM. That's the only purpose forReact.Fragment. It has no performance tuning at all.