Lurker News

Comments (156) 28 threads

Earlier comments
seanwilson3 replies#
I'm guessing diffing and updating the DOM is really inefficient if there's a lot of DOM elements and a few frequently updating part of your UI?

e.g. if you added a clock to the game in the article that updates every few fractions of a section to show something like "14.3 seconds taken so far".
The solution to this would be to make the clock into its own tiny self-contained component and to make diffing smarter so that only the DOM parts that belong to the clock need to be diffed/checked each clock update?

robertakarobin1 reply#
You would think that diffing is inefficient, but apparently modifying the DOM is significantly more inefficient. Mithril -- the framework that inspired this example -- has an interesting take: https://mithril.js.org/vnodes.html
websitescenes2 replies#
I think he means that this snippet of code doesn’t account for diffing like React does and would fail if scaled out. I think the OP is aware of that though.
TheRealPomax#
As a 33 line implementation "just for fun" exercise, no need to guess: this was not meant to be actual React in fewer lines of code =)
seanwilson#
I don't know how React does it so it was a genuine question. I'm assuming they do some smart diffing to be more efficient.

It'll be irrelevant for most apps but I'm curious how much less efficient all this is compared to (in CPU time, not developer time) updating the DOM directly e.g with:

      document.querySelector('.timer').textContent = "5 seconds"
leontrolski#
Mithril's approach (as robertakarobin refers to) is that diffing VDOMs is cheap (where as updating the DOM itself is expensive), the "library" in this post takes the same approach.

It's interesting, there's a lot of chat about performance with these frameworks, yet I'm not sure how much is relevant to the real world. When I do eg a

  document.querySelectorAll('*')
on airbnb map view (I guess a pretty good example of a mid complexity SPA), there are 3407 DOM elements - doing a diff on the VDOM elements of these should quick.

If I were doing something like a clock on a page (or an animation for example), I'd probably just do it out-of-band of my framework. These kind of things tend to be few and far between anyways.

learc831 reply#
That's why I prefer the Svelte way of doing things. It compiles down to code that directly changes the DOM, but only the parts that need to be updated.
jsf011 reply#
Do you know where to find docs on what svelte actually compiles to? I’m curious how they store/reference/update dom nodes in the compiler output.

Continue this thread — 1 reply

jmchuster1 reply#
If you came to this comments section looking for an actual lightweight standalone version of react, preact+htm works wonders.
lioeters#
I searched for "preact" in the thread, since I'm a big fan. Its small size and compatibility with React API make it suitable for use in "embedded widget" type situations, where a dependency on React would be too heavy. It's also perfectly suited for building full apps, or static sites. I've been using Preact on various projects for years, I love it.
k__1 reply#
Nice experiment.

I've also "re-created React" multiple times before it was actually created.
Diffing somehow always ended up being thr most natural way tondo things in the end.
My selling point in the end were the components

leontrolski1 reply#
My feeling is that if html and js had syntactically shared data structures at their conception, we wouldn't have quite so much frontend mess as we do now and declarative UI would have been the norm for a while. Having said that, I'm not sure how much precedent this has in UI frameworks outside of the web.
k__3 replies#
You think so?

What would be different if

    Document.createElement('a')
Would look like

    
?

The rest would still be imperative code.

capableweb#
The first one is imperative and locks the implementation to use the Document object and the createElement function.

The second one is declarative and leaves it up to the receiver to handle however they seem fit.
I'm not arguing one is better or worse, or that we would be better off if we we're using the JSX way with native support in the browsers, just explaining how I see the differences between the two.

colejohnson66#
It’s been a bit since I’ve used JS (so I may have the latter syntax wrong), but I’d prefer the JSX style of

    var elem = Test;
over

    var elem = document.createElement("a");
    elem.href = "#test";
    elem.innerText = "Test";
jQuery allows one to do something similar to JSX (which is nice):

    var elem = $("Test");
ken#
I don't interpret "html and js had syntactically shared data structures" to mean what you wrote, unless you also replace all other JS data structures (arrays and dicts and ...) with <> syntax. You've made it more concise but it's still distinct.

For an example of a system where the programming language and document generator share data structures, look at Hiccup (and Reagent). One of their very first examples is putting a loop inside a list. There's no special API for the attributes -- it's just an ordinary map. There's no special API for the children -- it's just an ordinary vector.

Load more comments