by spankalee · 8y ago · 0 comments
Where JSX would look like this:
const view = (state, actions) => (
<div>
<h1>{state.count}</h1>
<button onclick={() => actions.down(1)}>-</button>
<button onclick={() => actions.up(1)}>+</button>
</div>
)
The lit-html would be: const view = (state, actions) => html`
<div>
<h1>{state.count}</h1>
<button onclick={() => actions.down(1)}>-</button>
<button onclick={() => actions.up(1)}>+</button>
</div>
`;
Nearly identical. lit-html uses `<template>`s and cloning, so that it doesn't have to do any expensive VDOM diffs.So… it's not doing reconciliations and is just replacing the entire tree on every render, losing things like cursor position and forcing the browser to re-render and re-layout the entire thing?
Relevant part is at 8:30: "at first it will render the DOM and after that it will update what's already there"
I am wondering, how is it possible that such non-expensive algorithm is more expensive than many VDOM libraries in this benchmark[1] ?
1. https://rawgit.com/krausest/js-framework-benchmark/master/we...
1. benchmarks have zero information value
2. be careful about wording, react does effective DOM update, but effective does not always mean fast - what I mean is that compiled templates are usually much faster than doing vdom diffing along with all of that destructuring and other unoptimizable (prepack might help, but it's far from being usable) stuff
Statements without any proof is more valuable than "benchmarks with zero information value"?
> what I mean is that compiled templates are usually much faster than doing vdom diffing
If they are usually much faster, why it is so hard to make them perform faster than vdom libraries in this benchmark? Or maybe any other benchmark, please just show me something that I can measure.
Same principle, different implementation, takes advantage of special string literal properties to achieve impressive speeds.
choo https://choo.io/ uses tagged templates too.
It's 4kilobytes.
Wait, I thought VDOMs were supposed to be fast(er).
Note: I'm not a FE Dev, so I'm only going by stuff I (think I) read, not write.
Since updating the DOM is relatively fast in modern browsers it's not particularly hard to find cases where the work the virtual DOM has to do cancels out any savings.
1. See e.g. https://developers.google.com/web/fundamentals/performance/r..., a list of triggers at https://gist.github.com/paulirish/5d52fb081b3570c81e3a, and https://github.com/wilsonpage/fastdom for a common technique to avoid it by manually ordering read operations before writes.
Web Components and Shadow DOM make this possible. You can mix and match components that use lit-html, Polymer, Preact, etc., and mostly likely HyperApp.
I think within your own components it makes sense to stick with a single library setup for this reason, but atleast if you want to use a third-party component you don't have to rule out components that use different rendering machinery. You just have to weigh that additional download penalty.
Or is there tooling that works for the tagged template approach?