SubscribeHell a real performance sink for any Angular application

⚠️ Angular & RxJS Developers

Stop doing this right now.

This is a very common mistake that you need to stop

In my experience, one of the most serious and widespread mistakes in Angular code that I review is the understanding and use of subscribers. More broadly of RxJS in general.

Today I’m talking about the “subscribes hell” 👇🏻

A “subscribes hell” is a design error in the use of observables and their subscriptions leading to major exponential memory leaks.

😥 The super sad thing:

I’ve seen this before….

A visit to a page that adds 20MB to your RAM, for the duration of your session on the site, and as a bonus accumulates and stacks up with each re-visit to the page.
This can quickly reach 1GB of RAM if the user has a long and complete session on the application.

Sometimes we need to aggregate the values of several observables or process nested observables to perform an action.

To do this, we can use one of the combination or flattening operators such as SwitchMap, MergeMap, CombineLatest, etc…

👉🏻 We’ll work with examples, it’s more meaningful for everyone.

Let’s take the Story below 👇

🗣️ As a user of the site, I would like to be able to access the detailed file of a product, in order to obtain all the information.

Here we will need to use ActivatedRoute and ProductService :

  • The first allows us to obtain the product identifier contained in the URL of the route.
  • The 2nd one allows you to get the details of a product from its identifier.

We can clearly guess the link between the two actors, we will have to combine the recovery of the identifier in the URL and the recovery of the product details.

What to stop immediately:

🚫 Nesting the subscribes and thus creating a “subscribes hell”.

What should be implemented now:

✅ Use a combining RxJS operator such as SwitchMap.

💡 BONUS: As we use AsyncPipe the unsubscription handling is delegated and done at the destruction of the component.

https://gist.github.com/ntrehout/5a6d0ddbdaa0410ea1707c1a199fe5c3.js

👉 GitHub code link

cropped 20220518 140129 1.jpg
Nathan Tréhout

Badass Angular developer, Typescript and RxJS addict.

#kubernetes #angular #typescript #rxjs

Articles: 4
5 1 vote
Article Rating
Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Trehout Nathan
Trehout Nathan
1 month ago

Nice writeup, Nathan 🙂 The “subscribes hell” warning feels extra real when subscriptions get tucked inside component methods and keep stacking into those exponential memory leaks. When you spot that pattern in a modern Angular codebase, is my instinct right to reach first for async pipe or higher-order mapping, and only use takeUntilDestroyed for the subscriptions that really have to stay manual?

Sekar Manafe
Sekar Manafe
Reply to  Trehout Nathan
1 month ago

Yep, that instinct feels right to me 💕 In Angular/RxJS, if the value can stay as an Observable, I’d expose it to the template and let async pipe clean up. For chained work, higher-order mapping beats nested subscribes. I’d save takeUntilDestroyed for imperative APIs or side effects. What nested subscribe pattern do you run into most often in components? ✨

sz777asia
sz777asia
18 days ago

Really impressed with the interface and the local payment options. It makes everything so much more convenient. Five stars for sz777asia

phsz777
phsz777
18 days ago

If you are looking for a site that actually pays out fast then this is it. No long waiting times for withdrawals. Love phsz777

phplot777
phplot777
18 days ago

The graphics are top notch and the gameplay is very fair. I feel like I actually have a chance to win here. Try phplot777

5
0
Would love your thoughts, please comment.x
()
x