Thanks for the answer, thatās good to know! I looked at your code and immediately got few questions. I think we ended up with the same algorithm, so Iām curious.
Whatās happening when you more than 400 nodes on the page? Isnāt the rendering time too long? You are parsing the same styles over and over again at each rendering right?
We tried a solution like this:
- Write your HTML as styled nodes, and store styles in the node.
- Render the view function.
- Parse the resulting tree and extract the styles.
- Compute the styles and put them in a
style node.
- Add the
style node at the top of the DOM, add the view after this, and send everything to the runtime.
That was a perfect algorithm, perfectly working, and we even did some caching on styles to avoid recomputing styles twice (with a Dict). But we got a time rendering issue with large pages. When just writing some pages, it wasnāt a problem, because we had approximately
500ms for a rendering, and it was good. But we wanted to do some animations. We wanted 60 fps, so we needed at most 16ms in theory for each rendering. So we changed our strategy:
- Write your HTML nodes as usual.
- Write the style in
VirtualCss.style, which performs side effect in JavaScript.
- The style get computing if not computing previously (maintain a cache of computed styles), and pushed into the User Agent using
CSSOM.insertRule().
- The corresponding classes names are returned from the function, and they are put into the HTML via
Html.Attributes.class.
- The corresponding HTML is returned from the view and passed to the runtime.
This way, you avoid the two tree exploration. The only one needed is the runtime exploration. For huge pages, we ended up with 30ms of rendering. That changed everything to be honest.
Thatās why I think itās really important to talk about CSS and CSS in elm. Performances matter, and itās not usable for me if a page took more than 200ms to renderā¦
I would be glad to help in this or have a constructive discussion about how we should achieve this in the language.
Iām more than happy to see that @rtfeldman ended up with similar solutions, and to see that the idea of handling styles in elm is becoming more popular! 