godblessstrawberry
godblessstrawberry

Reputation: 5068

RxJS map operator vs destructuring performance

Personally I would consider #2 bad practice in terms of rxjs - am I right? Which of ways below is more preferable in terms of performance and why?

source$.pipe(
   map(s => s.someKey)
).subscribe(someValue => {
   workWithData(someValue)
})

or

source$.subscribe(({someValue}) => {
   workWithData(someValue)
})

Upvotes: 1

Views: 1146

Answers (1)

ganqqwerty
ganqqwerty

Reputation: 2004

I don't think that performance is the main question here, it's more about your intention. The second one is better because you call less functions.

If you plan to use the mapped observable stream in other parts of your application, you should use the first one, like this:

const sourceKeys$ = source$.pipe(
   map(s => s.someKey)
);
sourceKeys.subscribe(someValue => {
   workWithData(someValue)
})

If you will not need the sourceKeys$ as an observable, you can do all side effects and data operations in the subscribe block.

Upvotes: 2

Related Questions