News for hackers. Stuff that matters.
Re:The Cost of Frameworks by nostrademons on Tuesday November 17, 2015 @03:09PM (#10583609)
The DOM already does dirty-checking on manipulation. Manipulating the DOM is cheap, it's manipulating the DOM while also getting dimension or style information that's expensive.

None of the major frameworks solve this problem. The closest may be React, which strongly discourages you from interacting with real DOM nodes or doing anything stateful, but even then, if you really want to touch that offsetWidth you can trivially destroy React's performance. Maybe the best alternative is just strict coding standards that say "Don't do that!"

I'd talked with a few Chrome PMs when I was working on Material Design for Google Search and discussed the possibility of some sort of batching API for layout-causing operations. They weren't against it in principle, but nobody could think of an API that was something we'd actually use as web developers. The problem is that thinking about your app in terms of "This expression causes layout, this expression dirties the DOM" is a big cognitive load, and making those two classes of expressions non-interleavable means you suddenly need to structure your whole app in different ways.

There'd also been talk of doing a subset of HTML that includes only the operations that can be done quickly, but to my knowledge, that never went anywhere. You run into problems with the whole huge installed base of the web; if you're going to throw everything out and start from scratch, why not just use a native mobile app, or even raw OpenGL ES commands?

Re:The Cost of Frameworks by spankalee on Tuesday November 17, 2015 @06:44PM (#10584856)
Dart's html library used to make all methods that cause layout, be asynchronous. Instead of Element.offsetHeight, you had Element.offset which returned a Future<Rect> (Promise in JS). Code ended up looking like:

    fooify(element) {
      element.offset().then((rect) {
        // do stuff
        otherelement.offset().then((rect) {
          // do more stuff
        });
      });
    }
Understandably, developers didn't like it, and unfortunately it was removed. This was before Futures and Streams had been made completely ubiquitous in Dart, and before async/await. Now the code would look like:

    fooify(element) async {
      var h = await element.offsetHeight;
      // do stuff
      var h2 = await otherelement.offsetHeight;
      // do more stuff
    }
which is much more palatable.

> There'd also been talk of doing a subset of HTML that includes only the operations that can be done quickly, but to my knowledge, that never went anywhere. You run into problems with the whole huge installed base of the web; if you're going to throw everything out and start from scratch, why not just use a native mobile app, or even raw OpenGL ES commands?

Interestingly, this was basically the origin of Flutter. It started as a fork of Blink with the worst things removed, but still used DOM and JS, but they kept removing and removing, eventually removing CSS and the DOM entirely, and replacing JavaScript with Dart. Now things are fast, but they aren't like the web either.

Re:The Cost of Frameworks by munro on Tuesday November 17, 2015 @04:31PM (#10584096)
It's no surprise that mutating data structures is cheap, and React's virtual DOM doesn't aim to solve expensive layout calculation.

What virtual DOM does is simplify application development by removing the concept of time, so the programmer doesn't have to think about how to mutate the existing state to get to the new state. It's the cheapness of creating new virtual nodes that's interesting [1], and quickly applying them to the DOM with diffing.

[1] https://jsperf.com/virtual-dom-vs-real-dom

Re:The Cost of Frameworks by whatusername on Tuesday November 17, 2015 @07:38PM (#10585108)
Wasn't the subset of HTML AMP? https://www.ampproject.org/how-it-works/
Re:The Cost of Frameworks by nostrademons on Tuesday November 17, 2015 @07:52PM (#10585163)
I don't think so - it was different people working on it, from the Chrome team side, and it involved changes to Blink or an outright fork rather than anything server-side.
Re:The Cost of Frameworks by TimJYoung on Tuesday November 17, 2015 @05:21PM (#10584427)
Thanks for the response.

"The DOM already does dirty-checking on manipulation. Manipulating the DOM is cheap, it's manipulating the DOM while also getting dimension or style information that's expensive."

Yes, absolutely, and I wasn't clear on this point. The catch, however, is that once you start using JS for dynamic rendering of UI elements, as opposed to static HTML, this becomes quite the problem. Simple things like dragging an element around or other types of basic interactivity require knowing where things are in real time.

"None of the major frameworks solve this problem. The closest may be React, which strongly discourages you from interacting with real DOM nodes or doing anything stateful, but even then, if you really want to touch that offsetWidth you can trivially destroy React's performance. Maybe the best alternative is just strict coding standards that say "Don't do that!""

The only way I know of around this problem is what we did: maintain a virtual element for each actual DOM element, and have the virtual element act as a gatekeeper to the DOM. Want to read the width ? No problem, here it is as a simple integer property, and the DOM isn't touched. But, as you can imagine, this sends you down the road to hell pretty quickly, and we ended up managing everything manually. We effectively use the browser as a display device. And, of course, the code size is larger than we would like...and we're back to the topic at hand. :-)

"I'd talked with a few Chrome PMs when I was working on Material Design for Google Search and discussed the possibility of some sort of batching API for layout-causing operations. They weren't against it in principle, but nobody could think of an API that was something we'd actually use as web developers. The problem is that thinking about your app in terms of "This expression causes layout, this expression dirties the DOM" is a big cognitive load, and making those two classes of expressions non-interleavable means you suddenly need to structure your whole app in different ways."

Having come from many years doing desktop development on Windows, I've become accustomed to BeginUpdate/EndUpdate as it's a pretty standard way of batching updates that cause repaints. Of course, Windows also punts on layout, so there's a pretty big difference right there.

"There'd also been talk of doing a subset of HTML that includes only the operations that can be done quickly, but to my knowledge, that never went anywhere. You run into problems with the whole huge installed base of the web; if you're going to throw everything out and start from scratch, why not just use a native mobile app, or even raw OpenGL ES commands?"

I would say that you're leaving out a whole host of things that make the browser such an attractive host environment for applications, absent automatic layouts. There's the painting capabilities alone. Ever try to deal with transparency in Windows ? It's a nightmare, but in the browser it just works. The DOM element tree model itself is beautiful and a perfectly natural way of visualizing a UI. In general, it's just a more complete environment for the developer, and there's no arcane API to work around: access to the built-in functionality is just an object property away. Then there's the single codebase, easy distribution/installs.....

I just think that browser developers need to actively push towards making the UI functionality more generalized and display-oriented, and less specific to its roots of static documents that always require some sort of layout. If a piece of JS code places an absolutely-positioned element somewhere in the browser window, there should be zero penalty for reading its layout information after doing so.