r/javascript • u/ct_author • Apr 24 '22
What is DocumentFragment and createDocumentFragment() in JavaScript
https://codetopology.com/scripts/what-is-documentfragment-and-createdocumentfragment-in-javascript/[removed] — view removed post
20
u/KaiAusBerlin Apr 24 '22
Some improvements:
-Don't use var until you have to. Use const or let.
-document.createXY never adds anything to the dom automatically so createDocumentFragment has no special case.
-you could also use new DocumentFragment()
-the real improvement of documentFragment is that appending it (and it childs) just causes one single reflow which has a better performance than older techniques
12
u/peterrattew Apr 24 '22
Yeah the article was quite lacking when it came to explaining the benefits. Would be nice to see some benchmarks.
2
u/SplinteredResolve Apr 24 '22
I am learning JavaScript right now and was wondering if you could give me an example of a time when we should ever use var? Is there a use for it anymore?
5
u/KaiAusBerlin Apr 24 '22
Yep, if you target an older environment that doesn't support let or const. Or if you work with transpiled code.
6
u/bjerh Apr 24 '22
Or if you for some reason would like to have a variable hoisted from your bracket scope... But, hey... That means you're doing something wrong. 😅
0
u/getify Apr 25 '22
Here's some thoughts to consider regarding still using
varalongsideletandconst: https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/apA.md#the-case-for-var2
u/PM_ME_GAY_STUF Apr 25 '22 edited Apr 25 '22
This is potentially one of the worst articles I've ever read. Not only are the opinions largely unjustified or poorly argued, there are parts that are just factually wrong, e.g.
constnot preventing mutation, a take I see often that's a dead givaway that someone has never used a language other than JS or doesn't understand basic memory models. Because wanting everyone to learn the weird behavior forvaris totally justified but even a basic understanding of CS is a bridge too far1
1
1
u/pirateNarwhal Apr 25 '22
If I remember correctly, IE does not support
new DocumentFragment().1
u/KaiAusBerlin Apr 25 '22
If you still support IE you should definitely use a transpiler like babel to add automatically polyfills for newer technology like this.
3
u/Isvara Apr 24 '22
the document fragment is in memory and not part of the live DOM tree
While both of these things are true, it makes one wonder where the author thinks the live DOM tree is.
2
u/shgysk8zer0 Apr 24 '22
One of the biggest advantages I've seen to document fragments is using them indirectly via <template>. It's basically a necessity if you want to sanitize any HTML because other methods will actually load assets and add event listeners via on* attributes.
•
u/Ustice Apr 25 '22
/r/JavaScript tries to uphold a certain standard of articles in terms of quality and in spite of clickbaity titles. This post has been removed as a result of being deemed clickbait first, article second.
Thank you for your understanding!