Faster AngularJS Rendering with ReactJS
williambrownstreet.net
williambrownstreet.net
The issue of too many bindings he mentioned is a real one, and the complexity of this solution in general shows what sort of hoops you end up having to go through to get the basic stuff working (i.e. a for loop).
This is part of the reason why I wrote Mithril ( http://lhorie.github.io/mithril ) and the main factor for why I am planning on replacing Angular altogether for our mission critical app at work.
We do away with the client side model and complexity entirely and use partial HTML updates to get a better UX.
It isn't right for everything, but it is orders of magnitude simpler than Angular and it allows you to reuse many of the perf tuning tools we've developed over the last decade of web development.
Also, have you considered adding an entry to ToDoMVC project [1] for ease of comparison to other frameworks? I know it's not an exciting endeavour but it does make comparison much easier.
Thank you for creating it and your time in advance.
Re: TodoMVC - there was someone working on an implementation here: https://github.com/jpmonette/todomvc-mithril
I think it's not fully implemented yet, but you should be able to see the general look and feel of the code.
[1] http://lhorie.github.io/mithril/comparisons/mithril.renderin... [2] http://lhorie.github.io/mithril/comparisons/react.rendering....
I also do go more in depth here ( http://lhorie.github.io/mithril/comparison.html ). TL;DR: The main differences is that Mithril is a lightweight MVC, whereas React is a powerhouse at doing only the "V" part.
There's actually a project that uses React to enable HTML-y Mithril templates here ( https://github.com/insin/msx ), so the two can certainly coexist.
Mithril's light weight is mostly a result of picking a very small set of tools that are simple but useful on their own, but that also happen to come together nicely and that can play ball w/ Javascript's FP capabilities and the JS ecosystem as a whole.
Mithril is also a bit of a experiment in making a framework that gets out of your way - more hacking and less figuring out what is the syntax to do X. If that's your cup of tea, I'd recommend checking out the blog that I just started last week ( http://lhorie.github.io/mithril-blog/ ), where I talk about reusable programming techniques.
I load jQuery after Angular. This means Angular is still using jqLite - and prevents the extra functionality from being abused in simpler use-cases.
Sometime in the future browsers will support Object.observe natively. At this point I will scrap my companion directives and use Angular binding even in long lists.
http://plnkr.co/edit/Slowpr?p=preview
I also talked about this here. http://blog.stephenn.com/2014/03/javascript-apps-and-renderi...
Otherwise, I think it's well known by heavy angular users that some of the built-in angular directives are not necessarily the most efficient - building custom versions of directives like ng-repeat has been the current solution to squeeze out performance in edge cases, although it's unfortunate no one has decided on open sourcing such yet.
One thing I am confident about is when Angular 2.0 hits, things should improve immensely - the whole framework is moving towards modularity so you could presumably swap out the template engine for React, or perhaps the revamp of templating in Angular will make it more competitive performance-wise.
Here is the same demo, as in the article, with just 'track by $index" added to ng-repeat. As you can see, to the human eye, its just as responsive as the reactjs example.
Every `ng-*` and text interpolation using `{{ }}` will add one binding per usage. This can add up quite fast and easily cross the sweet spot of 2000 bindings per page (http://stackoverflow.com/questions/9682092/databinding-in-an...).
Large grids are not an ideal use case for Angular, and I like the approach of using React/Mustache or even direct DOM manipulation inside an Angular application when performance is critical.
The 2000 sweet spot comment from SO was based on angular 1.0.1, which didn't have 'track by' & is approx 1/10 the speed of angular now.
Look @ the updated jsperf digest test, from the SO answer, for angular 1.0.1 vs 1.2.14 http://jsperf.com/angularjs-digest/37
I've seen serious performance problems with Angular for even reasonably sized lists (granted, with fairly complex display logic) that React handled without making the browser hang and then scrolling lag noticeably. So I'd personally rather drop the 36KB of Angular before dropping the 32KB of React. ;) But if someone is just displaying a handful of elements with simple logic and is happy with Angular's performance, then there's no need to frankenstein React into it.
I feel like maybe Angular would be more convenient to program as an app, but I still have server-side logic -- my priority with a js lib is just to have a highly interactive UI even if it's just a fairly dumb state machine. To be honest, I feel like "dumb" has helped. I put just enough into state, just enough into events without being super model-oriented. Incidentally, I hear that is why Angular's perforance is poor (executes a bunch of js on model behavior even if it only results in 1 render -- not sure if this is true or not but I saw some interesting graphics of browser profiling that showed a ton of methods firing just for 1 small model update).
React is only 32kb(gzipped) in the production build (which also has performance optimizations that the development build does not).
We've also found that React helps make the rest of your application code much smaller, which may end up being even more important - but 32kb is certainly reasonable. We're extremely conscientious of page load speed and performance at Facebook which is why we place so much emphasis on React performance - second only to correctness and proper abstraction.
Mobile Webapps should provide feedback to the user as fast as possible on input. With long lists that has been quite problematic so far, and I have been looking for solutions to make the DOM update faster. ReactJs is a great help here.
btw, I am the author of this article.
ng-repeat="line in data track by $index"
Now try timing again.I've assumed it was a server side issue I could tune before pushing to production. Thanks, now I know where to look.