I am considering whether to use observables or promises in my new Angular 2 app. I am told observables can do anything promises can do and more, but I am curious as to how much overhead observables require compared to promises. Has anyone run benchmark tests? Are there any reasons left to use promises?
Asked
Active
Viewed 620 times
2
-
@GünterZöchbauer The title suggests that it is a duplicate, but the body of the question explicitly states that it addresses performance aspects which aren't covered by the duplicate. – Estus Flask Jul 28 '16 at 11:52
-
See also http://stackoverflow.com/questions/37364973/angular-2-promise-vs-observable – Günter Zöchbauer Jul 28 '16 at 11:55
1 Answers
2
Whereas both observables and promises deal with asynchronous processing, there are some differences:
- Observables can be canceled. Promises can't.
- Observables are lazy and are only executed when callbacks are subscribed to them. Promises are always executed when created.
- Observables can handle several events whereas promises can only be resolved once.
- Observables provide a set of operators to create asynchronous data flow (like
map
,flatMap
,filter
, ...)

Thierry Templier
- 198,364
- 44
- 396
- 360
-
Thank you for your answer, however I am more specifically interested in the overhead question. Are observables more resource hungry than promises, and if yes, how much? – rasmusrim Jul 28 '16 at 11:41
-
1You're welcome! I don't think that there are overheads when using observables. What you need to be careful is not to forget to unsubscribe subscriptions created when subscribing... – Thierry Templier Jul 28 '16 at 11:49
-
1The only problem might be the fact that you need to import rxjs to use observables, and even thought you can hand pick which parts you need. Rxjs still gets pretty large. But you are already using Angular 2 so you would need to import rxjs anyway. – Filip Lauc Jul 28 '16 at 13:03
-
@FilipLauc yes, you're right! You can select what you want to use with something like that: `import 'rxjs/add/operator/map';`. By default, only a minimal set is included. See this question for more details: http://stackoverflow.com/questions/34515173/angular-2-http-get-with-typescript-error-http-get-map-is-not-a-function-in/34515276#34515276. – Thierry Templier Jul 28 '16 at 13:06