News for hackers. Stuff that matters.
Re:The broken promise of Web Components by acdha on Wednesday March 15, 2017 @03:32PM (#13879045)
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.

Re:The broken promise of Web Components by jondubois on Wednesday March 15, 2017 @05:20PM (#13879886)
Yes, 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).

Re:The broken promise of Web Components by 31h on Wednesday March 15, 2017 @10:51PM (#13882050)
> JavaScript itself, it had some issues in the past, all the big issues were resolved

What about automatic semicolon insertion (ASI)? This makes it impossible to internalize how the JS implementation will interpret your code without memorizing a bunch of special cases, and makes "newline" semantically significant even if you only intend it for formatting your code.

It's never going to be fixed because of backwards compatibility. I can put up with a lot of language peculiarities, but I don't like wondering if some random "return" statement in my code is going to misbehave because of extra whitespace.

This is enough reason for me to keep looking for an ideal language.

Re:The broken promise of Web Components by Roboprog on Thursday March 16, 2017 @11:49AM (#13886005)
Possibly embrace the fact that semicolons are noise, and treat JS like you would Ruby or other line oriented languages??? (ignoring the fact that JS will "look ahead" past an otherwise complete line - e.g. - return [nothing] followed by open curly brace - but Ruby for example won't)

Remember the bad old days of BASIC? You only needed that extra delimiter (colon, rather than semi) when you were going to put multiple statements on one line :-)

Yeah, minifiers are often too dumb to accept valid input text :-(

Re:The broken promise of Web Components by 31h on Thursday March 16, 2017 @12:31PM (#13886366)
> Possibly embrace the fact that semicolons are noise, and treat JS like you would Ruby or other line oriented languages???

I'd be happy to treat JS as "statements end with a newline, not semicolon", if that were consistently true.

The paradox of Javascript: Do statements end in a semicolon, or newline? If you answer either way, you're sometimes wrong. As far as I can tell this is the only misfeature of JS that is really unfixable.

Re:The broken promise of Web Components by bigmanwalter on Wednesday March 15, 2017 @06:59PM (#13880718)
The React documentation says it so it has to be true. DOM slow, VDOM fast. Facebook is out there buying developer mindshare.
Re:The broken promise of Web Components by danabramov on Thursday March 16, 2017 @06:57AM (#13884010)
We 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.

Re:The broken promise of Web Components by bigmanwalter on Thursday March 16, 2017 @08:37AM (#13884487)
I 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.
Re:The broken promise of Web Components by danabramov on Thursday March 16, 2017 @11:20AM (#13885719)
I found these things hard:

* Inconsistencies or duplicated code between initial render and updates.

* Having to write code for N^2 transitions between N valid states instead of N valid states themselves.

* Harder to ensure DOM operation order across different parts of the codebase doesn’t produce jank.

Sure, this is all possible to solve, but in my experience I just ended up with a crappy version of React.

Re:The broken promise of Web Components by just2n on Thursday March 16, 2017 @07:12PM (#13889987)
Seconded on all counts. We also ended up independently implementing a really simplistic and in many ways broken version of React for these reasons and many others.
Re:The broken promise of Web Components by jbergens on Thursday March 16, 2017 @08:52AM (#13884563)
Probably 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.

Re:The broken promise of Web Components by bigmanwalter on Thursday March 16, 2017 @09:03AM (#13884630)
It's quite simple to handle that situation with jQuery and modules. If many parts of you code need to make a change to the DOM in the same place, you wrap the DOM changing in a module.

  var widget = require('./widget');
  widget.foo('bar');
And the widget handles the jQuery'ing for you.

  module.exports = {
    foo: function(s) { $('.widget').text(s) }
  }
Re:The broken promise of Web Components by dmitriid on Thursday March 16, 2017 @02:54PM (#13887901)
Only:

- it breaks immediately if there is more than one '.widget' on the page

- your app will get out-of-sync DOM updates because different parts will flush changes to the DOM at different times (React is DOM = f(state, time), so if multiple changes happen in f, they all be flushed to DOM in one go)

Re:The broken promise of Web Components by just2n on Thursday March 16, 2017 @03:36PM (#13888403)
In my experience it's the needed complexity added by bookkeeping and the coupling that requires to do rendering/updates and data binding using manual DOM manipulations in a performant way, especially in larger scale apps. You can easily write maintainable and simple applications that manipulate the DOM directly if you don't care about performance, or in some cases correctness, and you'll quickly do worse than React would, from what I've seen.
Re:The broken promise of Web Components by kbenson on Wednesday March 15, 2017 @07:50PM (#13881038)
> Yes, 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.

Honest question, because I use Javascript just infrequently enough to have to look this up every time (and it's possible the reported solutions are old), is there a good concise universal way (or two, at most) to determine the type of the variable I may have? It seems it's always a tossup between typeof, instanceof, == null and == undefined, and to my eyes that's way too many.

Re:The broken promise of Web Components by jdmichal on Thursday March 16, 2017 @03:19PM (#13888199)
As far as I know, `typeof` works on everything, but for Objects will only yield "object". You then should use `instanceof` to determine the type of object, if necessary. Typically, once I know it's an object I just use it, so that people can provide "non-traditional" but otherwise valid implementations. Only exception is if I'm specifically looking for a Date or DOM Node or other built-in type.

EDIT: Of course, I forget that arrays also have `typeof` "object"... Again, probably best to just start using length and indexing and hope for the best.

Re:The broken promise of Web Components by comex on Wednesday March 15, 2017 @08:50PM (#13881390)
A somewhat more succinct option could be to coerce to Object and then use instanceof: 'Object(x) instanceof String'. Or you could use Object.getPrototypeOf, which coerces to Object automatically these days. But null and undefined are special: calling Object on them returns {}, and calling Object.getPrototypeOf on them throws.

If the type you're testing for is not one of the builtin literal types, you can just use instanceof.

Re:The broken promise of Web Components by kbenson on Thursday March 16, 2017 @04:34PM (#13888923)
"Object(foo) instanceof Class" does seem to be the most definitive, but unfortunately seems to require specific testing against a literal that must exist or it throws, and doesn't work for null/undefined. That at least brings the things I need to know to check the type of something down to three, two of which are singular special cases (if somewhat common ones), so that's better and possibly adequate, if not what I would consider good. Then again, I'm not sure I'm capable of thinking it could achieve good while there exists both an undefined and null in the language. It still boggles my mind that it has both.
Re:The broken promise of Web Components by comex on Thursday March 16, 2017 @05:48PM (#13889522)
To clarify, by "literal that must exist or it throws" you mean that you don't know whether the class you're testing for exists? Testing against an invalid class would be an error in most languages…

I guess an alternative is foo.constructor and foo.constructor.name, which will give you a string if you want that.

Re:The broken promise of Web Components by kbenson on Friday March 17, 2017 @04:24AM (#13892403)
Since many different js libraries may be loaded from many different sources, I could envision a situation where some utility functions may want to support different inputs depending on what's passed in, and what's passed in might depend on what's loaded, and what's loaded can change at runtime.

That's why many dynamic languages allow access to the type through a string representation.

Re:The broken promise of Web Components by Roboprog on Thursday March 16, 2017 @11:55AM (#13886054)
Not really. Also, typeof doesn't work at all on object literals.

Best just to see if it quacks, or barks :-)

(i.e. - does the object define a given function/"method")

Re:The broken promise of Web Components by skocznymroczny on Thursday March 16, 2017 @09:11AM (#13884678)
except it still doesn't have support for modules in 2017.
Re:The broken promise of Web Components by bijection on Wednesday March 15, 2017 @06:39PM (#13880550)
Also, 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
  )
Re:The broken promise of Web Components by danabramov on Thursday March 16, 2017 @07:07AM (#13884057)
This 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.
Re:The broken promise of Web Components by dmitriid on Wednesday March 15, 2017 @03:44PM (#13879144)
Argh. 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 ---

Re:The broken promise of Web Components by acdha on Wednesday March 15, 2017 @08:03PM (#13881119)
Again, 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?

Re:The broken promise of Web Components by eschutte2 on Thursday March 16, 2017 @02:58AM (#13883093)
> Every time I've done a test, React is significantly slower than using standard JavaScript + DOM

I'd be interested in seeing those tests.

Re:The broken promise of Web Components by localvoid on Thursday March 16, 2017 @03:09AM (#13883130)
I 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.

Re:The broken promise of Web Components by acdha on Thursday March 16, 2017 @07:32AM (#13884154)
Try 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.
Re:The broken promise of Web Components by dmitriid on Thursday March 16, 2017 @03:09PM (#13888081)
Someone 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/

I highly recommend the author's article about it: https://medium.com/@localvoid/how-to-win-in-web-framework-be...

Re:The broken promise of Web Components by localvoid on Thursday March 16, 2017 @09:09PM (#13890536)
Also, 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/8a76ee942f329179d...
Re:The broken promise of Web Components by eschutte2 on Friday March 17, 2017 @01:50PM (#13896197)
Yes. If you're already checking for changes yourself then you've basically rewritten React.
Re:The broken promise of Web Components by jbergens on Thursday March 16, 2017 @08:54AM (#13884576)
Look at the tests for inferno, it is close to js + dom for some things and inside 2x factor for bascially everything if I remember correctly. The main thing is development speed anyway, fast enough while still easy to write and maintain code.
Re:The broken promise of Web Components by dmitriid on Wednesday March 15, 2017 @03:38PM (#13879092)
This 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.

Re:The broken promise of Web Components by BinaryIdiot on Wednesday March 15, 2017 @04:04PM (#13879294)
> 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.

Re:The broken promise of Web Components by floatboth on Wednesday March 15, 2017 @04:49PM (#13879643)
They're not even "its polyfills", the polyfills are a completely standalone project that Polymer depends on.
Re:The broken promise of Web Components by e111077 on Wednesday March 15, 2017 @06:32PM (#13880498)
If you look at the commits / maintainers to the Polymer repos and the WebComponents polyfills, you'll see that it's the same people.
Re:The broken promise of Web Components by Touche on Wednesday March 15, 2017 @04:31PM (#13879496)
Depending 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
Re:The broken promise of Web Components by e111077 on Wednesday March 15, 2017 @06:37PM (#13880529)
You 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.

Re:The broken promise of Web Components by dmitriid on Thursday March 16, 2017 @02:24AM (#13882963)
> JSX is very much virtualized template tags which are HTML standard

Nope, it's not. It's a thin XML-like DSL which translated into Javascript calls: http://bit.ly/2nC6fdu

Re:The broken promise of Web Components by spankalee on Wednesday March 15, 2017 @08:01PM (#13881111)
> You cannot buy into Web Components without buying into at least Polymer to supply you with data binding.

This is completely and utterly false. What are you basing your assertion on?

Re:The broken promise of Web Components by dmitriid on Wednesday March 15, 2017 @03:39PM (#13879097)
As 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/ind...
Re:The broken promise of Web Components by spankalee on Wednesday March 15, 2017 @08:06PM (#13881134)
So 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/ska...

Re:The broken promise of Web Components by BinaryIdiot on Wednesday March 15, 2017 @04:05PM (#13879304)
I 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.