3 ms·
The problem with not using a virtual DOM is that you can't merge in changes to stateful DOM elements. For example, an INPUT field has state (its value, plus its
by amelius 9y ago
The problem with not using a virtual DOM is that you can't merge in changes to stateful DOM elements. For example, an INPUT field has state (its value, plus its scroll position). If you regenerate the INPUT field from scratch, you risk losing the state. In contrast, if you use a virtual DOM, the vdom mechanism can merge in any changes for you (the INPUT field will stay the object!)
Of course, you can recreate DOM elements with their complete state, but if you'd try to do that with e.g. a VIDEO element, then you risk flicker and reloading of the actual video.
- naasking 9y agoI don't see why this would be any more difficult without a vdom. For the textarea, either way you're merging two text blobs, and if the vdom can maintain scroll position while doing this, then so can the non-vdom using the same mechanism.
- amelius 9y agoLet me try to explain. Let's say you have a textarea element which has DOM-state (value, scroll-position). And let's say you change the value through your render-framework. With the vdom approach, only the value component of the DOM-state is modified. With the non-vdom approach, you're creating a new textarea element, and set its value. Its scroll-position is lost, unless you keep track of it. Now with VIDEO elements, things are more problematic. If you recreate parent-nodes of a VIDEO element, the video will stop playing, or will flicker, or jump back to the beginning.
- naasking 9y ago> With the non-vdom approach, you're creating a new textarea element, and set its value. This isn't necessarily true. In fact, it's not true of Surplus and Radi.js, which is partly why they're much faster than every vdom framework out there.
- amelius 9y agoThey're faster because they are not computing DOM (or vdom) nodes when that's not needed. But they're creating new DOM nodes when values-on-which-the-code-depends change.
- lhorie 9y agoThis isn't quite correct. Modern rendering libraries aren't going to recreate an input from scratch unless they absolutely have to, vdom or not. What's tricky about reconciling app state changes and dom state is dealing with things like cursor position if e.g. the input value change comes from a socket and the user had the input focused and the cursor in the middle of the text. But this has nothing to do with vdom
- amelius 9y agoTo prove you wrong, consider how DOM elements are created (remember in Surplus there are no virtual elements). Ignoring all dependency tracking, the code to show some text has to be (eventually) of the form x = document.createTextNode(v); Now, consider the dependency v changes. The only option is to run the code above again, in its entirety. And thus, the DOM node corresponding to the text is new. Of course, a smart library will ensure that the amount of nodes that has to be visited is at a minimum. But that doesn't mean DOM nodes don't get rebuilt from scratch.
- merb 9y agowhat you discribe is basically what https://github.com/google/incremental-dom https://github.com/google/incremental-dom is all about and in certain cases it probably is as fast as vdom and also less memory heavy.
- lhorie 9y ago> The only option is to run the code above again, in its entirety Not at all. The reactive pattern typically boils down to something like this: el = document.createTextNode(initialValue); parent.appendChild(el); reactiveValue.onchange(v => { el.nodeValue = v; }) Ironically, in this specific example, vdom libs typically do this when possible: parent.textContent = v which does in fact replace the underlying text node, but they do so for the sake of performance
- amelius 9y agoYes, you can code like that, but ultimately that block will be contained inside a closure that will get triggered by some change. Of course, unless you are very careful.