6

Are there any differences when using the Pipe function with one argument, versus not using Pipe at all?

I am currently implementing the takeUntil unsubscribe strategy from this article. In the "official solution" from this SO question the takeUntil operator is sent through a pipe. However, on this page takeUntil is used with no pipe.

I am therefore wondering if there is any difference (memory leakage/performance, etc) in using a Pipe with a single Rx operator, versus no Pipe at all.

private destroy$ = new Subject();
...
this.potatoService.getPotato()
   .pipe(
    takeUntil(this.destroy$)
   ).subscribe(...

as opposed to

this.potatoService.getPotato()
    .takeUntil(this.destroy$)
    .subscribe(...
Anders
  • 403
  • 3
  • 17
  • Using pipe is Rxjs 6, not use Pipe is Rxjs 5, see https://www.learnrxjs.io/concepts/rxjs5-6.html, be carefull, the directory has changed – Eliseo Jun 25 '19 at 07:29
  • 2
    Using pipes makes your code tree-shakable. Failing to do so will result in an unnecessarily bigger codebase. – Andrew Hill Jun 25 '19 at 07:31

3 Answers3

2

There is no difference. The latter is the old way of using operators in RxJS. However, as far as I know, it is deprecated and you should not use it.

We used to prototype operators into Observables with static imports like this

import 'rxjs/add/operator/takeUntil';

However, this makes it impossible to tree shake RxJS. So that, RxJS announced pipeable operators starting from RxJS v5.5. Always use your operators within pipe

Bunyamin Coskuner
  • 8,719
  • 1
  • 28
  • 48
1

Since RxJS v6, takeUntil (and the others) have become pipeable operators rather than a standalone function.

In the link you shared, please have a look at the imports section which means this example uses a former version of RxJS:

import 'rxjs/add/operator/takeUntil';

From the official docs of RxJS v6, the import path of takeUntil becomes:

import { takeUntil } from 'rxjs/operators';

For further reading: https://rxjs-dev.firebaseapp.com/api/operators/takeUntil

Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
1

The point is that the old way adds the operator to the prototype so that every observable instance can use it. That’s why it makes the operator untreeshakable and it’s discouraged to approach that way.

DongBin Kim
  • 1,799
  • 5
  • 23
  • 43