9 ms·
Preact: Fast 3kb React Alternative with the Same API, Components and Virtual DOM
- runawaybottle 6y agoWhat does Preact sacrifice to get down to 3kb? Browser support? Nothing comes for free I guess, so just curious about the too good to be true nature of it. Why isn’t the React team putting in these enhancements?
- ylyn 6y agoThey sacrifice all the crazy performance optimisations that React has.
- vijaybritto 6y agoTBH preact is faster than React. I don't know of a scenario where react was faster.
- runawaybottle 6y agoHow do you measure? I’d love to see large renders and complex top level state updates that hit hundreds of components.
- williamstein 6y agoThis is relevant, though it is 2 years old: https://medium.com/@ajmeyghani/javascript-frameworks-performance-comparison-c566d19ab65b https://medium.com/@ajmeyghani/javascript-frameworks-perform...
- vijaybritto 6y agoI worked on a project where we were using React 14 before 2 years. It was only a desktop B2B site and later on when mobile support was inevitable we tried to use preact for mobile site since it was lightweight. We actually found that our desktop site which renders large tables with hundreds of rows and multiple chart components performed better in Preact without any optimizations. I'd say maybe a 20 - 30% improvement since we were seeing a gain FPS of around 10 frames. I think it would have been a bit more faster if we had used MobX instead of redux but that was too late.
- jamil7 6y agoHaven’t worked with React for a little while and only ever used MobX on one project, I was mostly using Redux. What performance improvements does MobX offer?
- vijaybritto 6y agoRedux always returns a brand new state. This will cause all consumers to rerender and react will update the dom only if there is a change. But with Mobx its more granular and the updates are atomic. This isn't a problem in apps where there is a single team working on it. But when you work in enterprise apps where different parts are worked on by different teams state updates to redux can be costly. This is not a limitation of Redux but a human problem that occurs inevitably. I've seen this in 3 different large scale projects. It starts well but after a year or so when people move out/in this bites us. This can be enforced with strict rules, eslint plugins and infra but they are expensive as hell. EDIT: 'This will cause all consumers (with incorrect mapStateToProps configuration) to rerender'
- jamil7 6y agoUnderstood, this is the reason I suspected and have spent considerable time in the past optimising state updates to forms in React and Redux projects so can imagine how this would become an issue in much larger teams than the ones I was working in.
- tga 6y agoWhen using Redux, components are supposed to select small parts of the state tree, and then they only rerender when those change. It is absolutely not the case that all components render on each unrelated update of the global state, that would be crazy. https://react-redux.js.org/next/api/hooks#useselector https://react-redux.js.org/next/api/hooks#useselector It is true that all of the selector functions need to run on each change, but those are supposed to be really small pure functions and their performance should be negligible, compared with everything else that normally happens in an app. Edit: It is absolutely possible to affect performance with Redux, especially on a large app — for example if hundreds of selectors need to run for every keystroke in a textbox.
- tsujp 6y agoThey have most of it listed in their docs here: https://preactjs.com/guide/v10/differences-to-react/ https://preactjs.com/guide/v10/differences-to-react/
- vijaybritto 6y agoThere are a few stuff that are missing from core preact that can be added via preact/compat package namely: memo, forwardRef, PureComponent, Event listener modifications similar to react, etc.,
- littlecranky67 6y agoThey don't do the event normalization/redirection. This is something you probably won't notice or need in your project anyways. I myself encountered the difference just once, when using Portals: In React, all events inside the portal would bubble/propagate to the parent components of the React component tree just as if the portal was mounted as a child of the surrounding component. In preact, all events in the portal propagate to the actual, real browser DOM hierarchy. I.e. if you have a click event inside the portal, the wrapping Preact parent component would never see the event or be able to handle this. This incompatibility would only be an issue when migrating to Preact from React; if you start with Preact from the beginning you would refrain from using this event bubbling to notify the enclosing component and use some other mechanism (i.e. forward the event data through a custom event handler or PubSub instance).
- jordic 6y agoSynthetic event system + render decoupling. Preact uses the event system of the platform (browser) and only renders to it (there's no react native support). Anyway we are using it at prod and allowed us to reduce our bundle sizes ;)
- eyelidlessness 6y agoWorth noting that while Preact specifically doesn’t support React Native, JSX does. So to the extent the dev APIs stay in line you can always swap out the underlying library for that target (or other renderers).
- kevsim 6y agoThe team behind Preact also recently released WMR [0] which attempts to simplify the process of actually building a web app. We're thinking of trying it out to replace the current mess we have with create-react-app, webpack, etc. 0: http://github.com/preactjs/wmr http://github.com/preactjs/wmr
- jokull 6y agoI found frontend tranquility in Snowpack and snowpack-rollup for production builds.
- kevsim 6y agoAny gotchas? When I read about Snowpack, I always get a too-good-to-be-true feeling. Maybe I'm just jaded by years of grunt, webpack, parcel, etc.
- dmitriid 6y agoFor starting from scratch it mist likely does wonders. Don't try converting a large legacy-ish codebase :)
- pea 6y agoWe love parceljs, too. Check it out.
- danmur 6y agoI like parceljs too. I haven't used it on big projects (plus I'm not a web dev currently) but I found it to be more "config when you need to change the defaults" than "no config". But that's great. It works out of the box for a lot of stuff and you add some dotfiles when you want to customise. I do want to try snowpack too though.
- hashkb 6y agoSnowpack got really really slow after I added a few dependencies. Also, it can't reuse its cache between a docker container and my laptop... I was hoping the cache would be portable but it's not. It needed more restarts than webpack. And you give away your freedom to tinker.
- littlecranky67 6y agoI love Preact, you'd have to look really hard to find actual, real-world differences that would make you use React over Preact. I find it funny that Preact also has a drop-in hook replacement `useErrorBoundary`, while React to this date still forces you to use class-based components if you want to establish error boundaries (using `componentDidCatch`).
- timdaub 6y agoI've been using preact with parcel for over half a year now and it has been a blast! It seems to be 100% compatible with React. It's fast, it's small. And with preact/compat you can use any third party react component. I'm a particular fan of https://github.com/developit/htm https://github.com/developit/htm, which is an alternative to JSX that simply uses JS template strings and needs no preprocessor.
- littlecranky67 6y agoIt is not 100% compatible, but it is very likely you will never hit those cases where it is not (see the other postings on this thread about it).
- hadrien01 6y agoGatsby has gatsby-plugin-preact which replaces React with Preact, and so far I've seen no difference in functionnality or speed, but the website's first load is slightly lighter.
- lecarore 6y agoFunny that this is on the front page today, I switched my project from preact to react in the afternoon. The main culprit : meteor supports react, not preact. I found a workaround at first but when getting a bit more serious it made sense to switch over to the mainstream and avoid unnecessary pains. Migrating from preact to react was easy, except for the render method arguments that I had to manually move over. Overall I like preact for small projects with little dependencies, and react for larger enterprise project where your just want as little pain as possible. Most react libs have to be forked to work with preact.
- awful_waffle 6y agoI reached for Preact on a project and was very satisfied with it. Until, that is, I hit a bug where DOM elements would be rendered out of order when I would call setState. I replaced Preact with React and everything worked fine. The logic was fairly straightforward as well - I was not doing anything fancy. Preact is great but be prepared for anomalies like mine.
- r0rshrk 6y agoMost probably this would be something to do with the key prop. Or how conditional components are being rendered. I can't make any judgements without seeing the final code though.
- developit 6y agoWe are working on a new renderer that started out life as a way of addressing the specific corner cases that can lead to this. It will be released in early 2021.