Live data from Hacker News

Comparing Svelte and React

jackfranklin.co.uk

291–300 of 338 posts

Re: Comparing Svelte and React

#291
post #207

Earlier quoted context omitted.

Thanks, that looks pretty good, and testing-library is excellent. I couldn't find it in the docs, but do you know how this "renders" the Svelte components - in memory using e.g. jsdom, or by firing up a full-fledged browser? (The former would be highly preferable.)

Testing-library uses jsdom across the board, as far as I know. It might support running in a full browser too but I haven’t used it that way. For end-to-end tests with a real browser, Cypress is a good option.

Cool, sounds like it should be in a usable state then.

Re: Comparing Svelte and React

#292
post #132

Earlier quoted context omitted.

Not necessarily. Discoverability is a function of colocation, not of immutability. One can write convoluted Redux monstrosities spanning a multitude of files where it's difficult to mentally follow how a dispatched action translates into state deltas (potentially because of too much higher-order abstractions, but also because one action may affect multiple reducers, and that in turn may affect a multitude of React tr…

These giant monstrosities are really easy to avoid. Redux Actions make locating a state change a breeze. I have worked on giant applications that did not use a redux-like approach to state and it was a horrendous mess.

FWIW, I see a lot of codebases at work (I maintain a large org-wide monorepo), and you'd be surprised at how daunting real world codebases can get. I agree that it's relatively easier to keep a codebase clean when the number of owners is small, but conversely, from my experience, when a codebase has multiple owners, keeping things organized can definitely be a challenge (in some cases I've seen, multiple teams owning overlapping sets of the codebase...)

I've seen codebases with messy redux and codebases with messy non-redux architectures, and conversely also clean codebases with both types of architecture. There really is no silver bullet technology. The "you can write cobol in any language" thing is always going to be a looming plague.

Re: Comparing Svelte and React

#293

Earlier quoted context omitted.

So I'm a Vue developer who hasn't ever actually built anything in Svelte, but Svelte uses a really cool model that's completely different from Vue and React. Where Vue/React use a virtual DOM, Svelte is compiled to essentially vanilla JS. Instead of having render functions that fully replace a component, Svelte updates just the parts of the DOM that need to be updated. Because of the dramatically reduced overhead, pr…

> Svelte is compiled to essentially vanilla JS How does this scale to anything remotely complex? I've found that if you have a decent knowledge of React it's hard to make anything slow with it.

Nothing beats vanilla JS in terms of DOM manipulation speed.

https://stefankrause.net/js-frameworks-benchmark8/table.html

And Svelte is fast. It's as fast or faster than React in most synthetic benchmarks.

For how and why, see https://svelte.dev/blog/virtual-dom-is-pure-overhead (notably the intelligent compilation step which removes need for vdom)

Re: Comparing Svelte and React

#294
post #288

Earlier quoted context omitted.

> It's very thin syntactic sugar over a function call Actually, it's not. It's a set of AST nodes. What they compile to is entirely implementation-specific. Look at Inferno.js or Solid.js compiled code for example; it looks nothing like simple function calls. Although obscure, there are also other spin-off ideas floating around like compiling to math expressions or to file system APIs. Even within the scope of React,…

> It's a set of AST nodes. Ok, but you could make the same point about plain JS as in Svelte. I haven't looked into it too much, but from the readme Solid.js looks more like a compiler than a library, so whatever it outputs when it sees JSX doesn't have much to do with what JSX is usually used for, imo. I've only ever seen it used as syntactic sugar for function calls, be it `React.createElement` or `h` or `jsx`. > A…

> Personally I find it much more likely that I'll have to bend over backwards when I'm dealing with a DSL rather than just plain code

I think this is cherrypicking at generalities. It's just as easy to run into a wall with "regular code" because some API doesn't provide what you need. Conversely, DSLs often have escape hatches (e.g. https://www.smarty.net/docsv2/en/language.function.php.tpl)

DSLs are useful sometimes. Heck, the entire web is built on top of DSLs: HTML, CSS, SQL, even JSX. I agree that there are valid criticisms to be made about specific implementations of specific DSLs, but I don't agree that those criticisms are generalizable to the concept of DSLs as a tool in a framework designer's arsenal.

Re: Comparing Svelte and React

#295
post #198

Earlier quoted context omitted.

> Redux is the purest expression of a state machine possible Are you sure "pure" is the right word? Last I checked redux very proudly incorporates a certain event-sourcing something idea in it. It's definitely not a "just state machine" library. More like in KFC when you order something, they always make sure that you get their sugar water as well.

What do you mean by "a certain event-sourcing something idea?" I really do mean "pure." That doesn't mean that Redux doesn't provide other facilities on top, such as React interop, error-handling, logging, etc. But if you don't need any of that and you use the core of Redux, it really is just a state machine representation. Let's write out the classic finite state machine representation of a locked/unlocked item. Sta…

If you think that's the simplest you could get, more power to you. I personally think your example is convoluted for such a simple example, but that's just me.

However, I should point out something that is not actually trivial about your code, by proposing a refactor: your FSM adequately models a car lock button interface, but not the lock itself. If we are modeling the lock, you have two invalid transitions...because it is impossible to lock an already-locked lock, and it is impossible to unlock an already-unlocked lock.

Classes actually shine quite well here.

    class On {
      constructor(){
        console.log('turned on')
      }
      turnOff(){
        console.log('turning off...')
        return new Off();
      }
    }
    class Off {
      constructor(){
        console.log('turned off')
      }
      turnOn(){
        console.log('turning on...')
        return new On();
      }
    }

    const toggle = new On();
    toggle.turnOff().turnOn().turnOff().turnOff()
    //     ^ cool,    ^ cool, ^ cool.   ^ oh shit, typescript really doesn't want to let me do this. 
Classes can model your original scenario quite easily (just add a `turnOn()` method to `On` and `turnOff()` to `Off`, but redux can't model my scenario (at least not without more boilerplate and lots of guards). More importantly, the available actions are localized...I don't have to create a monolithic state transition table logic...I only have to concern myself with the possible transitions for any given state. This makes it trivial to add new states or new actions, because I will never have to worry about the combinatorial explosion that can happen in a global transition table.

Re: Comparing Svelte and React

#296
post #143

I think it's a misunderstanding that Svelte is fast because it is "compiled" into imperative DOM updates. This is partly because the documentation says so, and the early blog posts emphasized this aspect. Compilation actually has nothing to do with it. Svelte is fast because it doesn't do all the Virtual DOM stuff, and the DOM diffing. You could very well build Svelte as a library without the compiler, and it would b…

I believe infernoJS complete with vdom still matches and even exceeds on most metrics.

Last I checked, svelte compiled different code paths for every single component.

This means that most DOM manipulations in real-world applications (not micro benchmarks using the same components repeatedly) won’t ever optimize in Svelte. Meanwhile, the vdom code runs constantly and is hand-tuned to help ensure deopts don’t happen. Likewise, potential optimizations to reuse old nodes and other “garbage” are much easier to implement with a unified DOM implementation. Finally, once you implement the vdom once, you don’t do it again. With Svelte, your code is recreated for every component. This may give smaller component in the short term, but there’s an inflection point (and it’s in the small-medium range) where svelte starts shipping more code for the same work.

Re: Comparing Svelte and React

#297

I really do not understand the appeal of Svelte, it seems to be making all the same horrendous mistakes as Vue (bad templating/DSL that shoehorns control flow and bindings into DOM representation, esoteric lifecycle hooks etc) and the only advantage it clearly has over other frameworks that I can see is simple components can generally be written more concisely. Are there actually any tangible benefits to using it ove…

- Instead of virtual dom + diffing, svelte applies direct DOM manipulation exactly where needed - Smaller code size. No loading of general runtime library, and I've heard people argue that each component(or page) has its own runtime (I may be wrong in the details) - Many common tools are built-in, like stores and animations, and since there's no general runtime, if you don't use a particular tool, it doesn't get incl…

Smaller code size only applies for small projects. Preact is like 4kb (I believe you could print off the minified source on one side of a sheet of paper) and most full vdom implementations are still only 40k or less over the wire.

Once you have the runtime, it’s a fixed size. In Svelte though, every new component must carry not just the description of changes, but also reinvent the wheel by reimplementing each time. How many components do you need to reach 4K or even 40k? Even a small app can easily reach the size inflection point.

Also note that inlining code works differently in a JIT. Your code must run hundreds of times to be optimized. In a vdom, that is done almost instantly for all the most critical render code paths. In contrast, infrequently used svelte components are never going to optimize. This has the effect of making svelte look very fast in micro benchmarks which run the same few components repeatedly, but doesn’t reflect your performance when the user opens up something different and winds up back in interpreted mode.

Preact is a nice middle if you’re concerned about garbage. In addition to being tiny, it diffs against the DOM directly instead of using a vdom.

Re: Comparing Svelte and React

#299

Earlier quoted context omitted.

There absolutely was a remote part to most VB6 apps. Usually they fetched data from some MS flavour of a RDBMS. Of course they were desktop apps and had to be installed individually but this “webify everything” mania seems to have smothered all dev productivity and experience on its altar.

Yes, and I remember precisely how many apps just encoded the DB credentials inside of the code - with no access/rights abstraction whatsoever. So basically godmode. Thats why we use service layers in Apps today (REST, SOAP etc.) instead of granting a user's process godmode access to the DB.

Why godmode? If your DB user is restricted to specific tables and columns, he is controlled.

If you eventually get access to the data, does not matter how many layers are in-between (REST, SOAP). You still got the data, so that's a "god mode" in the same sense.

Re: Comparing Svelte and React

#300

Earlier quoted context omitted.

The very important difference is that we do distributed systems now (admitted, X originally was also distributed, but only the drawing/events parts, not on the data level). A SPA is a distributed system, with all its drawbacks & pitfalls. If you ever did VB development in the 90s, you will know how easy it was to get a basic app running with those fancy Drag'n'Drop RAD tools. But there was no networking, everything w…

There absolutely was a remote part to most VB6 apps. Usually they fetched data from some MS flavour of a RDBMS. Of course they were desktop apps and had to be installed individually but this “webify everything” mania seems to have smothered all dev productivity and experience on its altar.

Exactly. Today's SPAs qualify as "thick client" from the old days.
Post reply on HN