Hey guys,
Just curious, does anybody know of a better way of diffing an inline template?
I have tried using the HTML template instead of script type = ‘text/template’ but the innerHTML.toString() causes it to be 30-40% slower. I am not sure if using HTML template is better for subsequent diffs as the browser parses the HTML.
//HTML
<body>
<script class = 'template' type = 'text/template' data-target = 'one'>
(template literals)
<h1>${this.obj1}</h1>
<h1>${this.obj2}</h1>
<h1>${this.obj3}</h1>
<h1>${this.obj4}</h1>
<h1>${this.obj5}</h1>
</script>
<div></div>
</body>
// JS
const render = (x) => {
// maybe JSON
let obj = {
obj1: 'One object',
obj2: 'Two object',
obj3: 'Three object',
obj4: 'Four object',
obj5: 'Five object' }
var text = document.querySelector('.template').firstChild.nodeValue;
let div = document.querySelector('div');
// Alternate to eval()
const templator = new Function("return `" + text + "`;");
let markup = templator.call(obj);
let frag = document.createElement('documentFragment')
frag.innerHTML = markup;
let app = () => div.insertAdjacentElement('afterend' ,frag);
requestAnimationFrame(app);
}
Thanks for your help/ opinions