7 ms·
The assertions about the DOM are long on hyperbole but rely on e.g. benchmarks from a decade ago and broad hyperbole like: “This was cumbersome. This was limit
by acdha 10y ago
The assertions about the DOM are long on hyperbole but rely on e.g. benchmarks from a decade ago and broad hyperbole like:
“This was cumbersome. This was limiting. You could not prototype easily and quickly. You could not easily escape the limitations of UI libraries. etc. etc. etc.”
It would have been far more interesting to look at whether the broadest claims were even true, or why people ran into problems using various libraries. Every time I've done a test, React is significantly slower than using standard JavaScript + DOM but that's the wrong question to ask when you really care about whether it's a) fast enough and b) helping you write code faster, reducing maintenance or sharing costs, etc.
All I got from this is that using React makes life easier than calling the DOM APIs directly every time, which is technically true but doesn't say anything about e.g. how WebComponents compare when using abstraction following the best practices of software developers since something like the 1950s.
The “easy-peasy” example is:
['Hello', 'world'].map((text) => {
return <p><span>{text}</span></p>
})
The alternative given is:
['Hello', 'world'].forEach(text => {
const p = document.createElement('p')
const span = document.createElement('span')
span.textContent(text)
p.appendChild(span)
MyComponent.appendChild(p)
})
Since the first example requires 140KB of React code plus a ton of setup code to get to that it's either careless or dishonest to compare the two without acknowledging the possibility of using a helper function or two to simplify the second one, as JavaScript developers have been doing routinely for multiple decades.
Again, not a slam on React, just this kind of unhelpful advocacy. It'd be a lot more interesting to compare real examples with multiple libraries and talk about how the different choices impact debugging, performance, isolation, etc.
- dmitriid 10y agoThis example shows how cumbersome the DOM APIs and their limitations are. Also, React is not _just_ DOM diff-ing. It also provides a host of other benefits... some of which are both shown here and covered immediately in the next set of examples with Polymer's kinda-templating-language. You cannot buy into Web Components without buying into at least Polymer to supply you with data binding. If you don't like React's size, there are slimmer options: Inferno, Preact etc.
- BinaryIdiot 10y ago> You cannot buy into Web Components without buying into at least Polymer to supply you with data binding. Why? You can use Polymer and not its data binding. In fact you can use its polyfills and deal with mostly the standard based APIs. X-Tags and many others also use Polymer's polyfills. DOM APIs are awkward as hell. But you can create Web Components and handle the data binding yourself. It's actually pretty easy to roll your own data binding. Now whether you should is another matter.
- Touche 10y agoDepending on your use-case you can use a variety of options for data-binding including 1) No data-binding at all (small components), or 2) An HTML-inspired data-binding solution like what Polymer provides or 3) A virtual-dom library like Preact or gasp even React
- e111077 10y agoYou don't have to use the DOM API for everything as in your example. JSX is very much virtualized template tags which are HTML standard. > This gets progressively worse as your components grow in complexity. Imagine adding a span around the text in p? In this case you simply type in the span inside the template tag similar to what you have in the React JSX and then just importNode the contents into your webcomponent.
- dmitriid 10y ago
- dmitriid 10y agoAs an example, you can't even build a simple ToDo List without Polymer's weird data-binding syntax: https://github.com/PolymerLabs/todo-list/blob/master/app/index.html https://github.com/PolymerLabs/todo-list/blob/master/app/ind...
- BinaryIdiot 10y agoI don't follow this logic. Because a Polymer example shows it using Polymer's data binding therefore you "cant even build a simple ToDo list without" it? That just isn't true at all. In fact you never have to be declarative.
- spankalee 10y agoSo you link to the Polymer TodoMVC example to show that it uses Polymer's data-binding, and this is supposed to show... what exactly? SkateJS's TodoMVC uses JSX[1]. Does that also imply that all Web Components need to use JSX? I don't see any logic in your claims. [1]: https://github.com/skatejs/todomvc/blob/skatejs/examples/skatejs/src/index.js#L103 https://github.com/skatejs/todomvc/blob/skatejs/examples/ska...
- dmitriid 10y agoArgh. I should learn to read/comment at once, and not split replies into multiple chunks :) > it's either careless or dishonest to compare the two without acknowledging the possibility of using a helper function or two to simplify the second one, as JavaScript developers have been doing routinely for multiple decades. To quote from the article: --- start quote --- DOM APIs are horrible, cumbersome, awkward and clunky. Polymer and others are bravely trying to use DOM APIs only, but even they resort to innerHTML anywhere they don’t have to put on a show (tests, for example). When Web Components take root, the web will be flooded with less performant innerHTMLs and possibly re-implementations of snabbdoms and virtual-doms (obviously incompatible) --- end quote ---
- acdha 10y agoAgain, why is the scary second half of that sentence an absolute certainty or bad? There's no law requiring use of innerHTML, and it's been easy to beat React on raw performance in many cases (to their credit, that's been getting harder but that's always a challenge for a general purpose library) — and in most cases none of that matters because you're not triggering updates fast enough to care. Ease of use and all of the other concerns are far more important for most people. Similarly, if people are building reusable components it's not clear why using different virtualdom libraries or not using one at all is a problem. Doesn't sound software engineering practice recommend a well-defined public interface rather than deeply interconnecting them like that? Besides, this is the web: what are the odds that you wouldn't quickly hit a problem where component X depends on v1.2.3 but component Y requires v2.3.4?
- jondubois 10y agoYes, it's like with JavaScript itself, it had some issues in the past, all the big issues were resolved but the stigma is still there. I can't see any fundamental reason why generating the DOM with React would be faster than generating the DOM with Web Components - They're doing the same thing; it's a matter of time before they even out (if they haven't already). I think that Web Components would end up faster ultimately due to native browser support (more room to optimise).
- bigmanwalter 10y agoThe React documentation says it so it has to be true. DOM slow, VDOM fast. Facebook is out there buying developer mindshare.
- danabramov 10y agoWe don't claim that in React documentation. React can't be faster than the same DOM mutations written by hand because by definition it has to do more work. We do think it helps to create maintainable apps though, and it is fast enough for practical use cases despite its immutable design. That's why we use it at Facebook.
- bigmanwalter 10y agoI just haven't been able to figure out what, exactly, it is about manual DOM mutations that makes an app unmaintainable. As long as your code is organized and modular, it's always been easy enough for me to navigate through and maintain a non-React codebase.
- jbergens 10y agoProbably finding the right place of the dom to change and keeping track of what to change. React basically says "make it like this" and takes care of a lot of things. jQuery code is well known to be hard to maintain when it changes things based on element-types, classes and id's nested in each other.
- bijection 10y agoAlso, for whatever it's worth, the js could be written more concisely as ['Hello', 'world'].forEach( text => MyComponent .appendChild(document.createElement('p')) .appendChild(document.createElement('span')) .textContent = text )
- danabramov 10y agoThis doesn't really help you with updates at all, only with initial mounting. In React, elements describe the current tree, and React figures out how to apply changes between the trees.
- eschutte2 10y ago> Every time I've done a test, React is significantly slower than using standard JavaScript + DOM I'd be interested in seeing those tests.
- localvoid 10y agoI also curious to see how web components will be faster than simple javascript objects :) React apps are usually built with many layers of components that returns another components. And with standard Web API, there will be significantly more DOM nodes because each component is a DOM node.
- acdha 10y agoTry beating a loop where e.g. every time you get a change you do something like getElementById() + .textContent = newValue. There's no way for React to be faster than that because it has to eventually do the same work but there are many ways for it to be less code, more correct (e.g. by not forgetting to call the update in some cases), or otherwise easier to get to the point where your app can ship.
- dmitriid 10y agoSomeone has tried beating the loop. And more than a loop, but rather a more real-life-like test rendering multiple components and component trees, sets attributes etc. etc. and you can run it on your own computer in the browser You can check it out yourself: https://localvoid.github.io/uibench/ https://localvoid.github.io/uibench/ I highly recommend the author's article about it: https://medium.com/@localvoid/how-to-win-in-web-framework-benchmarks-8bc31af76ce7#.9c6vng748 https://medium.com/@localvoid/how-to-win-in-web-framework-be...
- localvoid 10y agoAlso, it seems that many people are defending WCs with an argument about interoperability. But in reality, if we start adding components that are using different frameworks, there will be problems with layout thrashing, etc, because there is no standard scheduling APIs, and each framework implements its own scheduler to batch reads and writes https://github.com/ampproject/amphtml/blob/8a76ee942f329179d1b73d93d3de8d74bc464629/src/service/vsync-impl.js#L46-L47 https://github.com/ampproject/amphtml/blob/8a76ee942f329179d...