angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Build with "ng -prod" is extremely slow

Open ankeshdave opened this issue 9 years ago • 160 comments

Bug Report or Feature Request (mark with an x)

- [ x] bug report -> please search issues before submitting
- [ ] feature request

Versions.

ng --version _ _ ____ _ ___ / \ _ __ __ _ _ | | __ _ _ __ / | | | | / △ \ | ' \ / _ | | | | |/ _ | '| | | | | | | / ___ | | | | (| | || | | (| | | | || | | | // __| ||_, |_,||_,|| _||| |___/ @angular/cli: 1.1.0 node: 7.9.0 os: win32 x64 (Win 10 Enterpirise - 16Gig , i7) @angular/animations: 4.0.0 @angular/common: 4.0.0 @angular/compiler: 4.0.0 @angular/core: 4.0.0 @angular/forms: 4.0.0 @angular/http: 4.0.0 @angular/platform-browser: 4.0.0 @angular/platform-browser-dynamic: 4.0.0 @angular/router: 4.0.0 @angular/cli: 1.1.0 @angular/compiler-cli: 4.0.0 @angular/language-service: 4.0.0

Repro steps.

$ ng build --prod --aot=false -ec --sourcemap=false Hash: 552d9867ececa788ac3a Time: 670222ms chunk {0} polyfills.d77767cee98c83f90386.bundle.js (polyfills) 294 kB {5} [initial] [rendered] chunk {1} main.255e1d27fe4d40b56ab2.bundle.js (main) 1.51 MB {4} [initial] [rendered] chunk {2} scripts.45a4574f31d7b91e5a5e.bundle.js (scripts) 3.88 MB {5} [initial] [rendered] chunk {3} styles.ac17d766c993c4027892.bundle.css (styles) 228 bytes {5} [initial] [rendered] chunk {4} vendor.86c029ec4598fb674488.bundle.js (vendor) 8.23 MB [initial] [rendered] chunk {5} inline.750f0337ff0a9bee9813.bundle.js (inline) 0 bytes [entry] [rendered]

ng build --aot -ec -sm Hash: 24f744145f250126e699 Time: 62809ms chunk {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 294 kB {5} [initial] [rendered] chunk {1} main.bundle.js, main.bundle.js.map (main) 3.07 MB {4} [initial] [rendered] chunk {2} scripts.bundle.js, scripts.bundle.js.map (scripts) 3.88 MB {5} [initial] [rendered] chunk {3} styles.bundle.css, styles.bundle.css.map (styles) 228 bytes {5} [initial] [rendered] chunk {4} vendor.bundle.js, vendor.bundle.js.map (vendor) 7.37 MB [initial] [rendered] chunk {5} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]

670222ms vs 62809ms is a huge difference

The log given by the failure.

There is no failure , the build takes a very long time to complete its stuck at 92% chunk asset optimization and then we have to wait for 10-15 mins to get the build completed. I have disabled aot and sourcemaps, still the speed is very slow.

Desired functionality.

build should have similar time that to of ng build -aot

Any help and guidance to reduce will be helpful.

ankeshdave avatar Jun 24 '17 04:06 ankeshdave

I have also seen a huge increase since migrating to NG6:-

when running

ng build --prod --output-path ~/Desktop/KaiSuite/public
Date: 2018-07-14T09:30:25.804Z
Hash: d580dd49dcd4e6917e1b
Time: 607608ms
chunk {0} runtime.a66f828dca56eeb90e02.js (runtime) 1.05 kB [entry] [rendered]
chunk {1} styles.6e13815b16b2c751820b.css (styles) 572 kB [initial] [rendered]
chunk {2} polyfills.7a0e6866a34e280f48e7.js (polyfills) 59.6 kB [initial] [rendered]
chunk {3} main.8dfd3d83f3bae347a6a4.js (main) 1.02 MB [initial] [rendered]

That's 10min to compile less than 60 typescript files.

I also get the following warnings when building a projects now:-

WARNING in Invalid background value at 147:97. Ignoring.
WARNING in Invalid background value at 171:56164. Ignoring.

System info:-

Angular CLI: 6.0.8
Node: 8.10.0
OS: linux x64
Angular: 6.0.7

cd src/ && find -type f | sed -e 's/.*.//' | sort | uniq -c

      1 /browserslist
      1 gitkeep
     24 html
      1 ico
      4 jpg
      1 js
      3 json
      6 png
     24 scss
     59 ts

I tested using --sourcemaps but get the following error:-

Unknown option: '--sourcemaps'

and --build-optimizer=false results are no different.

thenslerse avatar Jul 14 '18 09:07 thenslerse

:arrow_up: I think it has changed to --source-maps @thenslerse

AshMcConnell avatar Jul 24 '18 07:07 AshMcConnell

Recent updates greatly improved the situation here. Note that the build takes quite some RAM, ~2Go here.

I don't know whats wrong with your build, but for reference here is my config:

angular.json config
$ ./node_modules/.bin/json -D'/' -f angular.json /projects/gestemps-front/architect/build/configurations/prod
{
  "fileReplacements": [
    {
      "replace": "projects/gestemps-front/src/environments/environment.ts",
      "with": "projects/gestemps-front/src/environments/environment.prod.ts"
    }
  ],
  "optimization": true,
  "outputHashing": "all",
  "sourceMap": false,
  "extractCss": true,
  "namedChunks": false,
  "aot": true,
  "extractLicenses": true,
  "vendorChunk": false,
  "buildOptimizer": true,
  "serviceWorker": true
}
Build output
$ find projects/gestemps-front/src/ -type f | sed -e 's/.*\.//' | sort | uniq -c
      4 eot
      1 gitkeep
    212 html
      1 ico
      1 jpg
      1 json
     48 png
    248 scss
     10 svg
    667 ts
     26 ttf
      4 woff
      4 woff2
$ ng build -c prod gestemps-front

Date: 2018-07-24T14:48:59.473Z Hash: ea9d61387d2af3ceabd9 Time: 99707ms chunk {22} 22.b59b168f3e26e58f571f.js () 7.26 kB [rendered] chunk {scripts} scripts.abc521399cddc9dfff22.js (scripts) 213 kB [rendered] chunk {0} common.f9ace648256335434c07.js (common) 37.8 kB [rendered] chunk {1} 1.5499a61f5e0ceef51961.js () 32.1 kB [rendered] chunk {2} 2.eba1d4227d25e1530c33.js () 18.1 kB [rendered] chunk {3} 3.e1f9d5b9a8f8e7380574.js () 23.7 kB [rendered] chunk {4} 4.d42fe05fbd7e1f8813f7.js () 108 kB [rendered] chunk {5} 5.23850a79a8216953a8a7.js () 226 kB [rendered] chunk {6} 6.4177f7ded01dad458a17.js () 24.1 kB [rendered] chunk {7} 7.3a4dadc888d3ecf86a20.js () 43.8 kB [rendered] chunk {8} 8.b5f37ef38c0266a0bff2.js () 26.2 kB [rendered] chunk {9} 9.a9f9f788ab594d3ecfdf.js () 16.2 kB [rendered] chunk {10} 10.e486711476a941ac9dec.js () 96.9 kB [rendered] chunk {11} 11.8e127c54d67e9335e8c3.js () 30.9 kB [rendered] chunk {12} 12.9f4ed703f1168dccf5cf.js () 80.1 kB [rendered] chunk {13} 13.6db615c37ecc172c5fa6.js () 30.2 kB [rendered] chunk {14} 14.052b7686366bb2f26d24.js () 38.3 kB [rendered] chunk {15} 15.e2245a819668c46a01c4.js () 37.5 kB [rendered] chunk {16} 16.7c535aedcaff7253bd08.js () 106 kB [rendered] chunk {17} 17.06a436c11b3a72156b8d.js () 112 kB [rendered] chunk {18} 18.e6819dd4cc3210927d4a.js () 39.7 kB [rendered] chunk {19} 19.d696738f06a391d0cb80.js () 39.3 kB [rendered] chunk {20} 20.3e01f7381cc58596f370.js () 92.2 kB [rendered] chunk {21} 21.ee46112cf361a8bca156.js () 7.4 kB [rendered] chunk {23} 23.e23d4566a33d8f9c8885.js () 43.9 kB [rendered] chunk {24} 24.aec6824d1689bdd4eb64.js () 25.3 kB [rendered] chunk {25} 25.8be5ea48dca99238076e.js () 62.8 kB [rendered] chunk {26} 26.7a0050c51f531d1139fd.js () 46.1 kB [rendered] chunk {27} 27.f2741b48d64773848311.js () 79.1 kB [rendered] chunk {28} 28.f706a121c03421ba3186.js () 51.8 kB [rendered] chunk {29} 29.6dc6da300b9570ccca0a.js () 47.1 kB [rendered] chunk {30} 30.bc47c5ac2ec1d443d19a.js () 56.4 kB [rendered] chunk {31} 31.9d8ed35e97ae1001f6c5.js () 91.6 kB [rendered] chunk {32} 32.3b4c316116b309503cb6.js () 116 kB [rendered] chunk {33} 33.8cfe4f67375db93fa434.js () 37.6 kB [rendered] chunk {34} 34.6ce051529c0407f5b008.js () 129 kB [rendered] chunk {35} 35.ba4d73fed5e6e11a29b4.js () 6.17 kB [rendered] chunk {36} 36.13018565227fa5fd45f9.js () 5.8 kB [rendered] chunk {37} 37.c14950ef72fdab47b533.js () 264 kB [rendered] chunk {38} 38.99504dfac9f13b5e20e9.js () 86.8 kB [rendered] chunk {39} 39.0bf52b854d08e0037da5.js () 367 kB [rendered] chunk {40} runtime.116953fa95d6d856c696.js (runtime) 2.85 kB [entry] [rendered] chunk {41} styles.10b82f02a98f259af35b.css (styles) 258 kB [initial] [rendered] chunk {42} polyfills.6f9dd0fcb5982023a125.js (polyfills) 101 kB [initial] [rendered] chunk {43} main.4d929acb0b2780c3ca2d.js (main) 2.13 MB [initial] [rendered]

Versions
Angular CLI: 6.0.8
Node: 10.6.0
OS: linux x64
Angular: 6.0.9

cghislai avatar Jul 24 '18 15:07 cghislai

Hey folks, I had an out of memory build issue and stumbled across a fix for the slow build (for us at least). It turns out, throwing memory at it fixed the problem. Here's our build command:

node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod --sourcemaps false

YMMV and it's a bit hacky but I hope it helps someone out there.

Brandon-Born avatar Aug 22 '18 04:08 Brandon-Born

Here's a data point for @Brandon-Born's suggestion. It decreased my project's prod build time from 50+ minutes to around 13 mins. 👍

Angular CLI: 1.6.3
Node: 8.9.4
OS: darwin x64
Angular: 5.2.1

haochi avatar Aug 23 '18 20:08 haochi

I had same issue.

The problem was in my app.scss file and the way I imported a library:

@import "../../node_modules/bootstrap/scss/bootstrap"
Time: 224909ms

vs:

@import "~bootstrap/scss/bootstrap";
Time: 19256ms
Angular CLI: 6.1.3
Node: 9.7.0
OS: darwin x64
Angular: 6.1.3

jpsk avatar Aug 27 '18 14:08 jpsk

This thread is just huge and I'm still subscribed to it just to know where are some weird culprits that blow things out of proportion without a discernible reason. Sorry for trying to invoke you @filipesilva but maybe the last comment from @jpsk can be one of the biggest culprits for the existence of this issue?

ShadowManu avatar Aug 27 '18 22:08 ShadowManu

@ShadowManu I had a try at @jpsk's comment on a new project and saw no discernable difference on a prod build (11544ms vs 11724ms).

Something I know can have a big difference is the version of uglify we use, which should be improved by https://github.com/angular/angular-cli/pull/11996.

Increasing the memory limit like @Brandon-Born does is not a hack, and something you should expect to do at some point. Node processes have a default memory limit of about 1.7gigs. When a node process starts getting close to the memory limit it needs to spend more and more time doing garbage collection to free up memory, which in turn makes it run more slowly.

Bigger projects will use up more memory than smaller projects so at some point a project will hit the memory limit. I personally use a npm script for this:

"ng-high-memory": "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng",

It's hard to act on many of these reports since there is no objective reproduction that we can follow. It's not that we don't believe that builds can be slow. But builds can be slow for many reasons. It's exceedingly likely that the projects that are seeing the most dramatic build times are suffering from some odd problem somewhere in the pipeline that is not a general problem, and for those we really need to reproduce it to fix it.

General performance improvements are of course always helpful, and we try to do them as much as possible. But the absolute best way of getting us to fix issues, especially hard to fix ones, is to offer concrete repros.

filipesilva avatar Aug 28 '18 15:08 filipesilva

Here's my latest numbers using the 6.1.5.

node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build --prod --source-map

Time: 3,117,385ms (52min)

ng build --prod --source-map

Time: 8,416,746ms (140min)

Hopefully the switch from uglify will really help this.

clabough avatar Aug 29 '18 17:08 clabough

The memory change/advice bought our prod build from 48 minutes to a cool 10. just by adding the max_old_space_size variable. Good work @Brandon-Born! Oddly we weren't seeing this on DEV's windows PCs. I'll await the terser change too, but this has been quite a useful page it has to be said.

inthegarage avatar Aug 31 '18 08:08 inthegarage

With these:

@angular-devkit/architect 0.8.1 @angular-devkit/build-angular 0.8.1 @angular-devkit/build-optimizer 0.8.1 @angular-devkit/build-webpack 0.8.1 @angular-devkit/core 0.8.1 @angular-devkit/schematics 0.8.1 @angular/cli 6.2.1 @ngtools/webpack 6.2.1 @schematics/angular 0.8.1 @schematics/update 0.8.1 rxjs 6.3.2 typescript 2.9.2 webpack 4.18.0

Running node --max_old_space_size=12000 ./node_modules/@angular/cli/bin/ng build --prod --source-map

I'm now down to Time: 470558ms (8 min).

Much better and tolerable. Seems like max_old_space_size should be configurable within tsconfig.app.json

clabough avatar Sep 10 '18 18:09 clabough

Just wanted to add that for my company's case, it was because our CI/CD server only had 2GB of RAM. With only 2GB, the build never finished. When we asked the CI/CD service provider to bump the RAM up to 3GB to 4GB, our build finished in roughly 3 minutes.

So if you are seeing this issue on your build server, but not on your laptop, first check how much RAM you have on the build server.

sivabudh avatar Sep 11 '18 05:09 sivabudh

@jpsk Thank you for your solution in https://github.com/angular/angular-cli/issues/6795#issuecomment-416236486

The fix was to use ~ instead of ../../node_modules/ in all of the scss project files.

In my case I had a build time from 2378409ms (~40 mins) and now it needs only 96053ms (~1,5 mins).

iCodr8 avatar Sep 23 '18 12:09 iCodr8

It's very strange for me - i have had 11 minute builds for the last month.. but 2 days ago i had some 40 second builds, and now back to 11 minute builds. I didn't change many things - so no idea how i got he 40 second builds..

robert-king avatar Sep 28 '18 02:09 robert-king

All of the OSX developers on our team were experiencing very high CPU usage, and very slow ng build and ng serve times (3-8 minutes for initial build and/or recompile). Bumping node to version 10.x via nvm, removing node_modules from the project, and reinstalling with an npm install dropped initial build time to about 30 seconds and recompile to about 12 seconds. This is on rather large, angular 6 application

kellycrowther avatar Oct 05 '18 04:10 kellycrowther

as by reading all the comments and seeing my own angular 6 build time, I'm concluding that UglifyJSPlugin at 92% is taking most of the time and RAM as well, it is consuming more that 2GB of RAM for just one build.

hbarve1 avatar Oct 10 '18 05:10 hbarve1

All of the OSX developers on our team were experiencing very high CPU usage, and very slow ng build and ng serve times (3-8 minutes for initial build and/or recompile). Bumping node to version 10.x via nvm, removing node_modules from the project, and reinstalling with an npm install dropped initial build time to about 30 seconds and recompile to about 12 seconds. This is on rather large, angular 6 application

Saved my day @kellycrowther . Thanks, m8. I confirm that on os x with nodejs v10.11.0 I can see a significant improvement

nirdshabo avatar Oct 10 '18 09:10 nirdshabo

same issue :(

Date: 2018-10-15T09:17:21.275Z
Hash: a8e9f7577a381cf027bd Time: 338960ms chunk {0} 0.e38e03f2d9c6fd65370c.chunk.js (common) 1.29 MB [rendered] chunk {1} 1.35b3fdbd653e9c99e87e.chunk.js () 168 kB [rendered] chunk {2} 2.78c04f64dba92e87b001.chunk.js () 12.7 kB [rendered] chunk {3} 3.ef5382adf15030bf4d7f.chunk.js () 41.1 kB [rendered] chunk {4} 4.ac5e7a11066bd3bce58a.chunk.js () 713 kB [rendered] chunk {5} 5.261500741b47979be46e.chunk.js () 867 kB [rendered]

aldrindizonh avatar Oct 15 '18 11:10 aldrindizonh

Same issue on OS X, last week it was around 10 mins but then something happened and now it's around 50 mins.. That's annoying... Going to try what @kellycrowther proposed

DaveLomber avatar Nov 06 '18 11:11 DaveLomber

Our build with these parameters (ng build --prod --aot=false --optimization=true --sourceMap=false --buildOptimizer=false) creates 71 bundles in just over 2 minutes with a total size of 11MB for all bundles.

Now we turn the --aot flag to true and what happens?? The build takes about 43 minutes, comes up with 84 bundles and has a total size of 22MB???? Isn't AOT supposed to minimize the size by excluding what is not being used? But instead it takes 41 minutes to create twice as many code? What's going on here?

negberts avatar Nov 09 '18 06:11 negberts