3 ms·
I think it's mostly performance as the browser can add performance optimisations for HTML but not unevaluated JavaScript. The alternative is a "display: none"
by apatheticonion 4y ago
I think it's mostly performance as the browser can add performance optimisations for HTML but not unevaluated JavaScript.
The alternative is a "display: none" block, but that triggers a calculation of styles, where <template> can never be rendered so it's evaluated (likely in parallel to JS) by the browser without style calculations.
The optimisations lose weight if the <template> tag is added programatically later. For it to be maximally efficient; all of the <template> tags must be inlined in your HTML prior to your application loading. You can play around with loading it asynchronously to ensure nothing blocks.
- spankalee 4y agoAdding a <template> tag later is great for perf too because the contents are still inert and fast to clone.