Live data from Hacker News

Virtual DOM in Elm

elm-lang.org

51–60 of 117 posts

Re: Virtual DOM in Elm

#51
post #33

Why so many strings and so few types, especially for somethings like Elm? The same in PureScript would be profile user = mkUI spec do div [ className "profile" ] [ img [ src user.picture ] , span [ text user.name ] ] See, types everywhere. https://github.com/purescript-contrib/purescript-react/blob/...

Because the underlying DOM is essentially untyped. This is a low-level adaptor library. One could easily build a more strictly typed wrapper on top.

Re: Virtual DOM in Elm

#52

There's another advantage to virtual DOM: you can hot-swap code while developing and have React run its diff algorithm without reloading the page. If your component has no (or little) side-effects, it means you get live reload as you edit for free. This is impossible with Backbone/jQuery soup of a view. See my proof of concept video: https://vimeo.com/100010922 And actually runnable example that you can edit without…

Yeah, the Om beginner tutorial demos this in lighttable with clojurescript, it's pretty epic.

edit: https://github.com/swannodette/om/wiki/Basic-Tutorial

Re: Virtual DOM in Elm

#53

All of the empty brackets to describe the virtual DOM looks bad to me. I'm curious if this is regarded as a language smell.

It's easy to use partial application to get rid of that problem: the type of the function node is:

  node : String -> [Attribute] -> [CssProperty] -> [Html] -> Html
We could define a convenient function div, for example, with

  div : [Attribute] - > [CssProperty] -> [Html] -> Html
  div = node "div"
that would let us say "div [] [] [text "Hello world"]" instead of "node "div" [] [] [text "Hello world"]". Of course, this doesn't fix your problem with the empty brackets. This can be fixed with something like:

  bareDiv : [Html] -> Html
  bareDiv = div [] []
letting us do "bareDiv [text "Hello world"]"

Re: Virtual DOM in Elm

#54

This technique may be the most revolutionary thing in web development in the last several years, IMHO. I've been using React for a while, and starting to integrate Mori for persistent data structures, and the things I can do with it is insane. The fact that it's not only far better performant, but a way better abstraction for dealing with UIs, is crazy.

What are you using Mori for that react.addons.update doesn't do?

Re: Virtual DOM in Elm

#55
post #28

Earlier quoted context omitted.

(a) You can use the key attribute in order to get DOM reuse. If you are looping over N keys then React is going to reuse the nodes and move them around. (b) You can implement shouldComponentUpdate in order to have a quick way not to re-render a sub-tree if nothing changed. (c) See (b) but we're also working on changing the internal representation of the virtual DOM to plain js objects that can be reused[1]. We were s…

I am currently writing an implementation of React in Scala.js. It's inspired by React and by the documentation of React, but I have not looked at the actual source code so far. You seem to be an implementor, so two questions that maybe spare me looking at the source code :-) 1. How do you batch updates? 2. I am currently using an algorithm for longest increasing subsequences for avoiding superfluous dom insertions of…

1. We buffer calls to setState() and apply them all at once (they don't trigger re-renders) and mark those components as dirty. Then we sort by the depth in the hierarchy and reconcile them. Reconciling removes the dirty bit, so if we come across a node not marked as dirty we don't reconcile (since it was reconciled by one of its parents).

2. I don't think we spend a lot of time trying to make this super optimal, but git grep ReactMultiChild to see what we do.

Re: Virtual DOM in Elm

#56

There's another advantage to virtual DOM: you can hot-swap code while developing and have React run its diff algorithm without reloading the page. If your component has no (or little) side-effects, it means you get live reload as you edit for free. This is impossible with Backbone/jQuery soup of a view. See my proof of concept video: https://vimeo.com/100010922 And actually runnable example that you can edit without…

What editor are you using in that gif? Or is that just 10.10 that makes it look "cleaner"?

Re: Virtual DOM in Elm

#57
post #28

Earlier quoted context omitted.

(a) You can use the key attribute in order to get DOM reuse. If you are looping over N keys then React is going to reuse the nodes and move them around. (b) You can implement shouldComponentUpdate in order to have a quick way not to re-render a sub-tree if nothing changed. (c) See (b) but we're also working on changing the internal representation of the virtual DOM to plain js objects that can be reused[1]. We were s…

I am currently writing an implementation of React in Scala.js. It's inspired by React and by the documentation of React, but I have not looked at the actual source code so far. You seem to be an implementor, so two questions that maybe spare me looking at the source code :-) 1. How do you batch updates? 2. I am currently using an algorithm for longest increasing subsequences for avoiding superfluous dom insertions of…

1. The boundaries are currently at event loop. Whenever an event comes in, we dispatch it to React and every time the user calls setState on the component, we mark it as dirty. At the end of that dispatch, we go from top to bottom and re-render elements.

It's possible to change the batching boundaries via "Batching Strategies" but we haven't exposed/documented it properly yet. If you are interested, you can look at requestAnimationFrame batching strategy. https://github.com/petehunt/react-raf-batching

2. We cannot really use normal diff algorithms for list because elements are stateful and there is no good way for React to properly guess identity between old and new. We're pushing this to the developer via the `key` attribute.

See this article I wrote for a high level overview of how the diff algorithm is working: http://calendar.perfplanet.com/2013/diff/

Re: Virtual DOM in Elm

#58

This technique may be the most revolutionary thing in web development in the last several years, IMHO. I've been using React for a while, and starting to integrate Mori for persistent data structures, and the things I can do with it is insane. The fact that it's not only far better performant, but a way better abstraction for dealing with UIs, is crazy.

What are you using Mori for that react.addons.update doesn't do?

react.addons.update is a poor man's implementation of something like mori. I'm not going to go into details right now (also still early in research), but mori is way more efficient and provides a better API for working with this kind of stuff (`sort` return a new obj, etc).

Re: Virtual DOM in Elm

#59
post #9
post #8

Earlier quoted context omitted.

I've seen some AngularJS vs React benchmarks a while back. I believe it was http://jsperf.com/angular-vs-react/5 I consistently got the result of Angular utterly and completely destroying React. Initially I blamed the virtual DOM approach, but after seeing other frameworks utilizing it and outperforming Angular by a huge margin, it seems to me that React is not written for performing well on small DOM documents. (The…

Our performance benchmarks suggest that application performance is most certainly better off with: (a) DOM reuse (b) calculating expensive things only once (c) reducing GC pressure == not discarding/recreating things (d) coordinating actions that may trigger reflow. It is independent of what framework you are using. My limited understanding of React is that it fails in (a), (b) and (c), and only limited measures can…

While each use case is different, I'd like to clarify a few things in my OP.

DOM reuse is not the same thing moving a DOM subtree to a different place. DOM reuse is e.g. getting an already-rendered table row, binding a new value to it, and modifying only the DOM properties in the complex DOM structure that actually did change. E.g. you modify only an Element.text deep in the first column, and a few other values in the other columns. Or maybe you need to do more, but all you do is delta. You don't just annotate a DOM structure with a key at row level, as it is closer to a hash of the DOM, not speaking of the data-dependent event handlers.

Calculating the DOM (virtual or not) is expensive, compared to not calculating at all. Creating a virtual DOM structure and not using it afterward creates GC pressure, compared to not creating at all. We are talking about optimizations in the millisecond range. A large table with complex components inside will reveal the impacts of these small things.

DOM coordination is not just making the DOM writes in one go. Complex components like to interact with each other, depending on their position and size on their page, and the changes in the underlying structure. They read calculated style values, and act upon those values, sometimes causing reflows. And if such things happen at scale, forced reflows may cripple the performance, and coordinating such changes may be more crucial than the framework you are choosing.

I am sure that people who are familiar with React may have their way get these stuff. I have looked at it, and I haven't seen it to happen automatically, while with Angular.dart, I get it without effort.

Re: Virtual DOM in Elm

#60
As a startup who's built all of its UI on AngularJS, does introducing React/Om into the stack make sense? We have a B2B product where the users deal with a lot of CRUD forms and dashboards.

React looks interesting, but only if it gives significant advantages (time-to-market, maintainability, etc) vis-a-vis AngularJS in managing a large code-base.

Any first hand reviews?

Post reply on HN