1

As I understand then combine multiple export statement into a single file is considered a good practice iff source file comes under the same directory

so

import { Observable } from 'rxjs/Observable';
import { Observable, BehaviorSubject, Subject, ReplaySubject } from 'rxjs';

can be re-written to

import { Observable, BehaviorSubject, Subject, ReplaySubject } from 'rxjs';

But what If I have to import only one module than which is preferred way? using a complete file or a specific one.

import { Observable } from 'rxjs/Observable';

OR

import { Observable } from 'rxjs';

does that cost anything extra?

ts v 2.3.4

angular v 4.4.6

xkeshav
  • 53,360
  • 44
  • 177
  • 245

1 Answers1

3
import { Observable } from 'rxjs/Observable';

This is the efficient way to import a single module, because you are providing the exact path to only fetch the required module.

Whereas in this case,

import { Observable } from 'rxjs';

It will import a lot of unnecessary stuff from the rxjs library that will increase the page load time and/or the code base.

Sagar Chaudhary
  • 1,343
  • 7
  • 21
  • Thanks. so instead of `import { Observable, BehaviorSubject, Subject, ReplaySubject } from 'rxjs';` we have to include separate modules statement is also preferred? – xkeshav Oct 03 '18 at 07:40
  • 1
    Yeah, thats correct. Just to save time and code ,we import all in a single statement. – Sagar Chaudhary Oct 03 '18 at 07:56
  • your solution in not valid for rxjs5 , t is mentioned [here](https://github.com/ReactiveX/rxjs/blob/master/CHANGELOG.md) > deep imports: Can no longer deep import top-level types such as rxjs/Observable, rxjs/Subject, rxjs/ReplaySubject, et al. All imports should be done directly from rxjs, for example: import { Observable, Subject } from 'rxjs'; – xkeshav Oct 29 '18 at 04:38