Difference between RxJS and IxJS?
As a complement to Oles Savluk answer, I found Matt Podwysocki's explanation particularly useful (https://gist.github.com/mattpodwysocki/1d0fe43961c6222571386568b8a5ef23):
we have four types of collections, each with their own purpose. Each has its own place, and there's no one solution that rules them all.
Pull: Iterable - purely synchronous data, either finite or infinite Push: Observable / Subject/Observer - eventual data such as DOM events, collections over time Pull/Push: AsyncIterable - I/O or other asynchronous data where the consumer needs to be in control Push/Pull: AsyncObservable - Network calls where creation/teardown may be asynchronous as well as projections may be asynchronous too.
Matt is a contributor to both RxJS and IxJS. Iterable and AsyncIterable come from IxJS, Observable and AsyncObservable are developed in RxJS
tl;dr
RxJS processes values as soon as they arrive. It's a push system.
IxJS specifies when to pass in the next value. It's a pull system.
Explanation
IxJS
may be helpful if want to have pull-based model, for example, when dealing with backpressure.
As you can see in the documentation:
IxJS unifies both synchronous and asynchronous pull-based collections, just as RxJS unified the world of push-based collections. RxJS is great for event-based workflows where the data can be pushed at the rate of the producer, however, IxJS is great at I/O operations where you as the consumer can pull the data when you are ready.
In other words:
- Use
RxJS
if your producer (usually User) is slower that processing of data (this is common for frontend). - Use
IxJS
if your producer (usually System) is much faster than you can process data (more common for the backend).
To understand what this means, consider the following example:
You need to build ETL pipeline and process a large file (about 1TB).
If you write it with RxJS, something like:
readFileByLineObservable('path/to/file')
.pipe(
doSomeHeavyTransformation(),
)
.subscribe()
Then readFileByLineObservable
will try to "push" the entire file of 1TB into RAM as soon as possible. Only after this occurs, you will start to do doSomeHeavyTransformation
. This problem is called backpressure.
In contrast, IxJS
will try to "pull" each newline only after previous line was processed. That's the optimal processing method in this case.
The difference is how RxJS's .subscribe
sets up a listener whereas IxJS's .forEach
tells its iterator when to give the next value (only after its done processing the first one. It's similar to, but not the same as, RxJS's concatMap
and concatAll
operators.