DEV Community

Discussion on: Learn basic Web Components

Collapse
 
bennypowers profile image
Benny Powers 🇮🇱🇨🇦

Shouldn't be innerHTMLing in constructor. Better to use the template

Collapse
 
dannyengelman profile image
Danny Engelman

Can you motivate that statement, Benny

innerHTML and append(Child) all do the same: they add content to a DOM

That .createElement"template") takes extra CPU cycles; content is parsed once.
With .innerHTML= content is also parsed once.

So there is only benefit if code does template.cloneNode(true) multiple times

Thread Thread
 
bennypowers profile image
Benny Powers 🇮🇱🇨🇦

if you know you're only going to use the element once, yeah there's no difference.

But if you're going to construct the element several times, then you're going to invoke the parser on each instance.

If the element's shadow DOM is extensive, that could add up fast.

Moreover, keeping the template static means it's parsed up front.

Collapse
 
dannyengelman profile image
Danny Engelman • Edited

If you don't take my word; then maybe you take this guy his comment on innerHTML

I don't know the guy personally .. but Justin Fagnani sounds familiar 😛