I am in need of clarification about the efficiency of operator pipelines in RxJS.
My current knowledge about the operator pipeline in RxJS is that each operator inside the pipeline receives an observable and creates a new (possibly modified) observable which gets returned to the next operator as input. This behaviour would be similiar to the JavaScript filter, map, reduce behaviour. Thus leaving the original observable (stream) or array untouched/pure.
This assumption is supported by the RxJS Documentation at: https://rxjs-dev.firebaseapp.com/guide/operators
A Pipeable Operator is a function that takes an Observable as its input and returns another Observable. It is a pure operation: the previous Observable stays unmodified.
Considering long operator pipelines the creation of intermediate observables seems kind of expensive to me.
In addition I am reading the book 'Reactive Programming with RxJS 5' written by Sergi Mansilla. I know that RxJS is currently at version 6.5.3 but I expect that the basic mechanism did not change since then.
In the book there is a section about the efficiency of pipeline which says that observable pipelines do not create intermediate Observables. Instead they apply all operations to each element in one go. Which makes sense since the operator take(amount) completes the observable stream after taking the first amount elements. It also explains the lazy evaluation traversing the source observable stream only once at max or until the take condition is met.
import * as rxCore from 'https://dev.jspm.io/rxjs@6/_esm2015/index';
import * as rxOps from 'https://dev.jspm.io/rxjs@6/_esm2015/operators';
const numberStream = rxCore.range(0, 10);
numberStream.pipe(
rxOps.map(number => number * number), //creates Observable with [0,1,4,9,16,25,36,49,64,81]
rxOps.filter(number => number % 2 === 0), //creates Observable with [0,4,16,36,64]
rxOps.take(3) //completes after [0,4,16]
).subscribe(console.log); //logs 0, 4, 16
Are there any intermediate observables being created inside this operator pipeline? Or is only the complete pipeline creating one new observable leaving the numberStream untouched? Or what exactly is the case?