ryansolid / dom-expressions

A Fine-Grained Runtime for Performant DOM Rendering
MIT License
863 stars 125 forks source link

Lazy template creation #214

Closed titoBouzout closed 1 year ago

titoBouzout commented 1 year ago

From time to time, one questions if templates should be created when used, instead of at start-up. Currently, solid creates all the templates at startup, regardless if these are going to be used or not.

In my opinion, templates should be created when used.

Pros:

Cons:

Alternatives:

Please note that this change also includes caching, which means that any template that has the exact same shape will be reused and not regenerated.

Real life testing:

function templates created time
current template 153 3ms
improved template 23 0.5ms
titoBouzout commented 1 year ago

I came with the template caching by trying to figure out how to make it lazy. Wasn't intentional, it just happened. I'm not sure if it is worthy, as each call to cloneNode now means it will do map.has, map.get, and then cloneNode, template duplication doesn't seem to happen as frequently. So it's been removed from this pull request.

ryansolid commented 1 year ago

I appreciate the PR and the testing. The plan is to incorporate these improvements assuming they do well in my testing but I've taken it a bit more radically including improvements to compilation and code reduction. Expect to see this in 0.36.x.

Thank you for inspiring this.

ryansolid commented 1 year ago

Good news is this doesn't appear to have any negative impact on simple DOM benchmarks. Like if you show a table with 1000 rows.. and instead you now have to create it one extra time at 1001 it isn't move the dial very much. So that's positive. But lets hope this improves startup and hydration metrics. I think for SSR this is way more interesting because it means that we may never instantiate the template.