News for hackers. Stuff that matters.
Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by gambler on Thursday March 14, 2019 @03:45PM (#19392845)
>Virtual DOM libraries provide a declarative interface to the Web’s imperative DOM.

I'm not sure what this means. DOM is an object model for HTML. It is mutable, but HTML itself is definitely declarative.

Which brings up an interesting question. Why are DOM-diffs something that is done by userland libraries when it can and probably should be done by the browser itself?

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by bfrydl on Thursday March 14, 2019 @06:06PM (#19394312)
HTML is declarative, but it's also a string. It is faster to manipulate the DOM to make the changes you want than to generate an HTML string and ask the browser to parse it.

A virtual DOM allows you to use declarative _code_ instead of a string to get the same benefits as declarative HTML, but avoids the overhead of HTML by manipulating the DOM directly under the hood.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by KuhlMensch on Thursday March 14, 2019 @04:26PM (#19393303)
As I understand it - please correct me if I'm wrong!

> HTML itself is definitely declarative

HTML is the markup (not programming) language (which is Javascript). The API to actually change the DOM is imperative. See https://developer.mozilla.org/en-US/docs/Web/API/Node (I don't quite grasp why they felt the need to specify that)

> Why are DOM-diffs something that is done by userland libraries?

I guess if the browser provided primitives to build trees and diff them that would be cool. But I presume using WASM *is getting the native-browser code to do the diffing.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by sraquo on Thursday March 14, 2019 @04:22PM (#19393249)
Because virtual DOM is not the only way to achieve efficient DOM updates, and even within the concept of virtual DOM there could be many different ways to achieve efficient diffing (e.g. React vs Snabbdom). There is nothing special about a particular virtual DOM spec to deserve a place in web standards.
Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by dmitriid on Thursday March 14, 2019 @05:55PM (#19394208)
Web standards could specify an API (which is very simular across all virtual dom inplementations) and let browsers chise their own implementations and optimisations.

Since the browser has full access to the entire object model, and memory layouts, and dom optimisations, and..., and..., it’s a shame that we have to write code that is the prerogative of the browser.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by sraquo on Thursday March 14, 2019 @06:17PM (#19394408)
There is enough differentiation in virtual DOM designs[1] that I don't think implementing a lowest common denominator that is flexible enough to support competing strategies would be useful. Being a browser API it would need to be a rather timeless, low level design. Essentially, we already have that – the imperative DOM API.

With WASM the performance gap between libraries and native browser code will be reduced even further. Focusing on that has the benefit of lifting all boats, not just a particular DOM building paradigm that has been popular recently.

[1] Just some examples that come to mind:

- logic that decides when to update an element vs when to create a new one (including but not limited to having the concepts of components, thunks, deciding to look at class names, ability to move nodes, etc.)

- design of lifecycle methods and hooks, as well as any performance-oriented optimizations such as asynchronous / delayed rendering, short circuiting logic, etc.

- handling of props vs attributes vs reflected attributes

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by dmitriid on Thursday March 14, 2019 @06:27PM (#19394490)
> Essentially, we already have that – the imperative DOM API. With WASM the performance gap between libraries and native browser code will be reduced even further.

Why waste time implementing and “reducing it even further” and not just implement it in the browser?

> Just some examples that come to mind

First bullet point is relevant for internals mostly, little bearing on actual API.

Second bullet point is nearly identical in all virtual doms and similar lifecycle hooks exist in WebComponents (which squandered the opportunity to introduce a declarative API).

Third bullet point is valid, but the main problem isn’t the difference between library APIs. The main problem is that there is no browser API, so everyone has to reinvent the wheel. A simple {attrs: {}, props: {}} would render the differences moot (or you would have very thin wrappers on top for your favorite syntax)

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by sraquo on Thursday March 14, 2019 @08:13PM (#19395210)
> Why waste time implementing and “reducing it even further” and not just implement it in the browser?

Because the browser should not offer inflexible implementations of complex, highly opinionated and currently overhyped paradigms, because those standardized APIs will stay with us for much longer than they will be useful, wasting everyone's time.

We're only talking about virtual dom here because it's a popular concept with good library implementations. We don't need to reimplement it in all major browsers because we already have it working well.

Even aside from that, it is easier to build one library than implement the same spec in all major browsers. Moreover, library designs compete with each other, and can be improved faster than APIs baked into a browser which will have to be maintained in a backwards compatible manner for more than a decade.

> First bullet point is relevant for internals mostly, little bearing on actual API.

The concepts of Components, State, Context, Thunks, Fibers, Plugins, etc. are very important differentiators between various virtual DOM APIs. Either the presence or absence, let alone the specific design of those concepts strongly affects the API surface and what users can do with it and how. Don't mistake React's API for some kind of standard.

Once the hype inevitably moves on from virtual DOM to whatever the next declarative UI paradigm will be (e.g. FRP with precision DOM updates) this whole standardization and reimplementation exercise will be rendered a giant waste of time.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by dmitriid on Friday March 15, 2019 @03:53AM (#19397539)
> because those standardized APIs will stay with us for much longer than they will be useful, wasting everyone's time.

Oh wow. You just described the existing imperative DOM APIs, haven't you? Inflexible implementation wasting everyone's time. When is the last time you used actual DOM APIs? The story of web development has been: "JFC, these things are impossible to work with, let's waste time creating actual useful abstractions on top of them".

> it's a popular concept with good library implementations. We don't need to reimplement it in all major browsers because we already have it working well.

You know what one of the goals of jQuery was? To become a disappearing library. As browser APIs got better and better, the need for jQuery would diminish and it would disappear, becoming just a browser API.

Why would browsers implement querySelector and querySelectorAll? We already had it in jQuery and it was working well.

Why would browsers implement fetch? We already had jQuery.ajax, axios, superfetch and dozens of others, and they were working well.

Why would browsers implement <insert any improvement>? We already have <insert doezns of libraries> and they are working well.

> it is easier to build one library than implement the same spec in all major browsers.

It doesn't mean we should freeze the spec in the same state it was in 1998.

> The concepts of Components, State, Context, Thunks, Fibers, Plugins, etc. are very important differentiators between various virtual DOM APIs.

None of those refer to actual APIs. Those are parts of internal implementations or additional implementations/additions/APIs on top of virtual dom.

We are talking about one thing specifically: we need a browser-native declarative DOM API with browser-native DOM-diffing that wouldn't require us implement it in userland.

The rest like thunks, state management, plugins, whatever can be provided by actual libraries on top of actual built-in high performant built-in virtual-dom API.

Because the browser knows infinitely more about what's happening to the DOM than userland libraries and has access to infinitely more optimisations. All userland code needs to do is to tell the browser: this and that changed.

Funnily enough, browser implementors are now spending considerable amounts of time implementing CSS Containment [1] (emphasis mine):

--- quote ---

Browser engines can use that information to implement optimizations and avoid doing extra work when they know which subtrees are independent of the rest of the page.

Imagine that you have a big HTML page which generates a complex DOM tree, but you know that some parts of that page are totally independent of the rest of the page and the content in those parts is modified at some point.

Browser engines usually try to avoid doing more work than needed and use some heuristics to avoid spending more time than required. However there are lots of corner cases and complex situations in which the browser needs to actually recompute the whole webpage.

--- end quote ---

Wow. Browsers (and browser implementors) actually want the developers to tell them what exactly changes on the page so that they don't do extra work. And wow, you can actually implement the same spec in all major browsers (eventually).

So why not virtual DOM?

> this whole standardization and reimplementation exercise will be rendered a giant waste of time.

So what you're saying is essentially this: new things are hype, DOM APIs should never get updated because who cares about the needs of developers.

[1] https://blogs.igalia.com/mrego/2019/01/11/an-introduction-to...

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by sraquo on Friday March 15, 2019 @04:35AM (#19397767)
> Oh wow. You just described the existing imperative DOM APIs, haven't you? Inflexible implementation wasting everyone's time.

Existing DOM APIs are very simple, low level and unopinionated. A pleasure to build libraries on. That's what durable platform APIs should look like.

It is very easy to build virtual dom and importantly other DOM management paradigms on top of those low level APIs.

The same can not be said about virtual dom - it's a very opinionated, very rigid paradigm. I know because I built FRP UI libraries based on Snabbdom and on native DOM APIs. The latter is much simpler to deal with and more performant. Virtual DOM only works well if that's exactly what you want. It has no place among browser APIs, at least not in any recognizeable shape or form.

Regarding performance, the whole point of the virtual dom is that the diffing engine does not know which elements changed or didn't change. It gets a new virtual subtree and has to diff it with the previous one. The browser would be doing all the same diffing work, just closer to the metal. But we will soon be able to do the same with just WASM.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by dmitriid on Friday March 15, 2019 @04:59AM (#19397869)
> I know because I built FRP UI libraries based on Snabbdom and on native DOM APIs. The latter is much simpler to deal with and more performant.

I built something on native APIs and on userland APIs. Native APIs are more performant.

Really? That surprises you?

> The browser would be doing all the same diffing work, just closer to the metal.

Exactly my point

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by tracker1 on Monday March 18, 2019 @03:45PM (#19424456)
The things a VDOM get you is a simpler interface than the browser DOM, with an often faster comparison than the browser actually provides. There have been significant real browser DOM improvements since React came out, but I'm pretty sure an optimized VDOM in WASM could be faster because of issues of interaction with the real/full browser DOM. There are also side effects wrt the full/real DOM in practice.

I agree though, nothing that requires a place in web standards at all.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by sraquo on Monday March 18, 2019 @05:17PM (#19425413)
Well... faster than what exactly? You have to do the virtual DOM pattern (generating new DOM state and then diffing with old state) with virtual elements. You can't compare proper virtual dom to using real DOM elements instead of virtual ones in a virtual dom pattern, it wouldn't make any sense.

But there are other non-virtual-DOM ways to manage DOM state efficiently and in a maintainable manner. For example, my own library uses Observables to drive precise DOM updates and works with trees holding real (not virtual) DOM elements, so it doesn't need to do any diffing at all: https://github.com/raquo/Laminar

I don't think it's a given which of these techniques would be faster, it depends heavily on the particular use case and the implementation of diffing (for virtual DOM) and Observables (for my pattern). If both are well optimized I'd expect virtual DOM to lose in a lot of cases.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by tracker1 on Monday March 18, 2019 @05:31PM (#19425534)
Like I said, could, it probably depends on actual use... DOM navigation for read or update can be optimized, but depending on how it is done may not work as well. React itself is moving towards diffing against the browsers real DOM iirc. Browsers have gotten a lot better than in the past. That said, actually comparing each node for updates against large trees may be more costly than updating and diffing against a partial abstraction.
Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by sraquo on Monday March 18, 2019 @05:41PM (#19425620)
What I'm saying is outside of the virtual DOM paradigm you might not need to diff any elements at all, real or virtual, and so you wouldn't care about the performance of DOM reads, as you're not doing them.

Then it becomes a matter of DOM write performance, but that is the same for everyone assuming the native DOM API commands issued by the libraries are the same, which is a more or less reasonable assumption for well optimized libraries even if they use different paradigms to calculate what those commands should be.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by socceroos on Monday March 18, 2019 @07:39PM (#19426488)
Templates are faster than vdom and are a browser feature now.
Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by mikekchar on Thursday March 14, 2019 @07:32PM (#19394979)
Lots of good answers, but the one thing I noticed nobody has mentioned (unless I missed it) is that "DOM" stands for "Document Object Model". The HTML is the "Document". The DOM is an object oriented programmatic interface to the document. The DOM was used not just for HTML, but also for SGML and XML (if you use libraries like Sax).

So the statement you have quoted is slightly confused (although a very common confusion). People think of the DOM as being essentially an AST of the document tree. The "Virtual DOM" is seen as kind of scratch pad for that tree: update the "virtual DOM" and the AST is automatically brought into sync. At that point the "DOM" (as Document Object Model) loses any meaning because it is no longer that object model (with it's completely insane API) that we all love to hate.

Why are these AST diffs being done in userland libraries when it could be done by the browser itself. Because you already have a DOM. Why would you possibly want another programmatic interface? ;-) Though tongue in cheek, this is really historically the answer. The API you've got is the API you've got.

Providing an ability to get the document as an AST and to update it by furnishing a new AST would be wonderful. But you've got to get it by the standards committee.

Edit: I should point out that browsers used to support XSLT which allows you to transform the document declaratively based on the AST. Not sure if any browser still supports it though...

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by kccqzy on Thursday March 14, 2019 @05:31PM (#19393991)
Because once the declarative HTML has been transformed into DOM objects, they are not just dumb data but OO-style rich objects. They have identity and internal state, which may or may not be reflected by their markup (not even innerHTML or outerHTML). For example when you attach an event handler, that object gains this behavior. If perhaps that object has some other change, and a DOM-diff library stupidly deletes the original DOM and add a new one, that could be lost. The same for typed text in <input> fields.

Simply put, the OO nature of the DOM complicates things. If DOM were just a simple recursive tree data structure of which HTML is a serialization, there would be no such issues.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by est31 on Thursday March 14, 2019 @05:36PM (#19394028)
Virtual DOM is needed to enable a programming model where in each UI iteration, you return the entire UI tree anew. Actually creating a new DOM tree anew would be terribly wasteful, and you might lose lots of state (e.g. half entered text or selected text or something) so instead you do dom-diffing. For some reason, people consider that model to be more hip than the normal one where you take care of updating yourself. Probably because writing code to create the entire UI tree anew is more easy to write, but tbh it's also more wasteful. Classical ease of use vs computation tradeoff.
Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by AgentME on Thursday March 14, 2019 @08:38PM (#19395355)
>For some reason, people consider that model to be more hip than the normal one where you take care of updating yourself.

A huge benefit of the virtual dom model as used in React is that you don't need to have separate code paths for the initial render of a widget and updating part of a widget. I can't count the number of times I've seen pre-React widgets coded to assume most attributes would never change, had bugs in updating some uncommonly-updated attributes, or gave up on having separate initial-render/update code paths by throwing away the widget's entire DOM and re-rendering on most attribute changes. With the React way, there's one code path for initial render and updating, so it doesn't take any special effort to make every attribute efficiently update-able. A whole category of bugs disappeared in codebases I've worked on that adapted React.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by groby_b on Thursday March 14, 2019 @09:13PM (#19395559)
It's not because it's "more easy to write". It's because code that generates the entire tree every time can be side-effect free, while stateful UI is, well, stateful.

We can debate if that's good or not, but it's not about "ease of use". Neither is it necessarily more wasteful. It's a different paradigm.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by kreetx on Thursday March 14, 2019 @03:53PM (#19392936)
My guess is that the desired DOM is entirely re-created from some state, instead mutating it in place. The declarataivity then is that "given this state the DOM should be this".
Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by AgentME on Thursday March 14, 2019 @08:31PM (#19395305)
If something gets standardized, then it becomes hard to iterate on the details. You can't make incompatible changes to browser standards, or else you break compatibility with old web pages.

React is currently working on figuring out how to adapt their API to allow asynchronous updating of the DOM. If the virtual DOM was standardized already, then the React team would have been very limited in how they could implement asynchronous rendering.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by Hello71 on Thursday March 14, 2019 @04:03PM (#19393031)
1. This is called "Shadow DOM", it is only supported in the latest browsers. wasm is AFAIK slightly more widely compatible.

2. Even if calls from wasm to the browser are as fast as JS, they will still incur overhead above "internal" function calls.

Re:Fast, Bump-Allocated Virtual Doms with Rust and Wasm by anonytrary on Thursday March 14, 2019 @05:32PM (#19393998)
Shadow DOM has nothing to do with this.