Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any difference between import { Observable } from 'rxjs/Observable' and import { Observable } from 'rxjs'?

Tags:

angular

rxjs

When using rxjs in angular 2, is there any difference between import { Observable } from 'rxjs/Observable' and import { Observable } from 'rxjs'?

like image 899
techguy2000 Avatar asked Feb 07 '17 07:02

techguy2000


People also ask

Which is used to import Observable?

import {Observable} from 'rxjs/Observable'; would import the Observable, but you don't need to import it all if you are using promises... toPromise works without it.

What is Observable from RxJS?

RxJS introduces Observables, a new Push system for JavaScript. An Observable is a Producer of multiple values, "pushing" them to Observers (Consumers). A Function is a lazily evaluated computation that synchronously returns a single value on invocation.

What is difference between observer and Observable in Angular?

Observable are just that — things you wish to observe and take action on. Angular uses the Observer pattern which simply means — Observable objects are registered, and other objects observe (in Angular using the subscribe method) them and take action when the observable object is acted on in some way.

What is the difference between Observable and a subject in RxJS?

An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast. A Subject is like an Observable, but can multicast to many Observers.


1 Answers

Yes there is a slight difference which is the bundle size. If you aren't using tree shaking library like rollup.js which remove all unnecessary codes, your bundle will be large when importing from 'rxjs' as you are importing everything even if you are using only the Observable. On the other hand if you import from 'rxjs/Observable' you are only importing what you need and the bundle will be smaller.

Import only what you need and patch Observable (this is useful in size-sensitive bundling scenarios)

Ref: https://github.com/ReactiveX/rxjs

like image 63
Sabbir Rahman Avatar answered Oct 10 '22 05:10

Sabbir Rahman