Investigating long TypeScript compile times

I’m investigating why the compilation time for my Angular 2.0 TypeScript project went from around 4 seconds to around 15 seconds in a relatively short time.

I came across the very useful, but seemingly undocumented --diagnostics switch.

For example, here’s what I get when running tsc --noEmit --diagnostics on my project now:

Files:             231
Lines:           50872
Nodes:          170067
Identifiers:     65994
Symbols:       7712123
Types:          407677
Memory used:   600554K
I/O read:        0.43s
I/O write:       0.00s
Parse time:      1.13s
Bind time:       0.34s
Check time:     10.17s
Emit time:       0.00s
Total time:     11.64s

Here’s what I get when I run the same command on an earlier version of the project.

Files:             197
Lines:           30882
Nodes:          124208
Identifiers:     46201
Symbols:       5856945
Types:           10989
Memory used:    80412K
I/O read:        0.03s
I/O write:       0.00s
Parse time:      0.60s
Bind time:       0.27s
Check time:      0.93s
Emit time:       0.00s
Total time:      1.79s

The number of Types has gone way up, and so has the Check time.

Is it possible to get more detailed/verbose output from --diagnostics?

NodeJS v4.4.3, TypeScript v1.8.10. This is my tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",

    "noImplicitAny": false,
    "noEmitOnError": false,

    "experimentalDecorators": true,

    "emitDecoratorMetadata": true,
    "removeComments": false
  },
  "exclude": [
    "node_modules",
    "wwwroot",
    "typings/main.d.ts",
    "typings/main"
  ]
}

5
Leave a Reply

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Seems I have found the culprit in my case. I did it the hard way; my process: Find the commit that made the compilation slow. Go through the history commit-by-commit and check the compile times. Comment out the changed code until the offending lines are found. Before the offending commit, I consistently got compile times of around 2-4 seconds, after the commit – 13-17 seconds. In my case, I have a class, with a accessTokenGetter field, which was initialized in the constructor: export class JwtConfig { //... accessTokenGetter: () => Observable<string>; //... constructor(private config?: IJwtConfig) { // ... this.accessTokenGetter =… Read more »

Jason
Guest

I could speed up the compile process from 15 sec. to 6-7 sec. by changing this single line of code: // slow: // ... .flatMap((receivedObj: MyType) => { let nextObservable: Observable<MySecondType> = this.dependingPut(receivedObj); return nextObservable || new Observable((observer) => { observer.next(undefined); }); }); // fast: .flatMap((receivedObj: MyType) => { let nextObservable: Observable<MySecondType> = this.dependingPut(receivedObj); return nextObservable || new Observable<MySecondType>((observer) => { // <--- use the generics! observer.next(undefined); }); }); From the Typescript handbook (https://www.typescriptlang.org/docs/handbook/generics.html): function identity<T>(arg: T): T { return arg; } // ... let output = identity("myString"); // type of output will be 'string' “Notice that we didn’t have… Read more »

Jason
Guest

For me, slowdowns were due to imports like import "./file.ts";. Removing the .ts extension makes things 90% faster: import "./file";

Jason
Guest

Compiler is known to be slow, you can use compiler in watch mode tsc -w or switch to webpack and ts-loader which expose transpileOnly option (no time consuming checks).

Jason
Guest

In my case compile time was around 15-30 seconds. After tsc --noEmit --diagnostics command I’ve noticed that I’m building 2k files. Solution was to exclude all files from node_modules folder:

{
  "compilerOptions": {...},
   "exclude": [
     "node_modules"
   ]
}

It is a good idea to exclude typings folder too.