Repository navigation
Description
TLDR: We want to improve webpack's performance in our development environment, and after investigating many performance options we are looking for direction on how to make progress. We want to bring our cached production build and our webpack development server boot times down from 2m58s and 2m28s to ~15 seconds.
NOTE: Please don't close the issue because I deleted the template, if it's important leave a comment and I'll add the template w/ answers to this issue.
Some background on our build:
We have 4 build configurations
| Name | Description | Chunks | Entrypoints | Modules |
|---|---|---|---|---|
| Client | Builds our bundles with the browser in mind. | 710 | 587 | 15314 |
| Server | Builds a subset of our client bundles with slightly different ways of handling async loading, externals, etc. | 259 | 259 | 11152 |
| Embed | Builds a few files meant to be embedded on non airbnb sites (no externals, commons chunks, or code-splitting) | 10 | 10 | 1151 |
| AMP | Builds a few amp specific files to help ensure we're AMP compliant (I'm sure compliant is the wrong term). | 2 | 2 | 1928 |
Current production build timings
A full build with no babel/happypack cache and without uglify takes: ~4m31s
A full build with a cache, but no uglify takes: ~2m58s
Timing for restarting the webpack server and loading our homepage
These are measured in our ci system, which is slower than our actual development environment.
Current Master- full page load, 147673.2 ms (~2m28s)
Optimizations that we've investigated
Production build optimizations already in place
- happypack - This saves us time in our production builds, we parallelize/cache our babel handlebars transforms. We don't use this in our devServer as babel-loader has a built in cache and that is far and away our most expensive loader.
- webpack-parallel-uglify-plugin - This parallelizes/caches the uglification process. On a fully cached build this takes ~3 seconds.
Development server optimizations already in place
- We boot our dev server with only a small subset of our build in place as it's unlikely a developer will need to compile all of airbnb.com locally. We start with any bundles needed for our CommonsChunkPlugin, and add addtional bundles to the compiler as they're needed. This helps significantly to reduce memory pressure as well as initial boot time, and rebuilds.
- Any server bundles that are added to the server compiler automatically trigger a compile for client side equivalents, it's assumed if you server render something, you'll also want the client code.
Build optimizations that help dev and prod
- Disabled symlink resolving, this helped both our dev boot times as well as our production build times.
- babel-loader cache - This is also beneficial for production builds, but is hugely helpful in development as well.
devtool: 'cheap-module-eval-source-map'
Other stuff that we've tried
Our primary focus has been on dev server boot up time, as we believe this will have the highest impact on our developers productivity.
- hard-source-webpack-plugin - We tried this plugin during its 0.4 releases. It was very promising at the time, shaving 25% off of our dev server boot time. We encountered lots of random caching errors though, and it often resulted in manually deleting the cache and restarting the server to correct. Also, we recently revisited this plugin and didn't see a performance improvement in our local environment, it's impact may have been dimished by recent webpack performance work. We also have not revisited this plugin's newer releases.
- cache-loader - Reduces our dev server boot up time by ~3%. We assume this would have a larger impact without babel-loader's built in cache.
- thread-loader - Increased our dev server boot up time by ~57%.
- dll plugin - At the time of investigating this was incompatible with the CommonsChunkPlugin, this may have changed. We didn't see much performance improvement here, and didn't think it was worth the risk of development/production build differences.
Things we've considered but haven't tried
- Disabling CommonsChunkPlugin in development, manually tracking module dependencies for bundles, and caching this information on disk. This would help our boot time as we could cache build results, and verify that they're still correct based on config/module changes. This comes at a cost though as bundles can't be compiled correctly (matching production) without also compiling with CommonsChunk targets in place.
Profiling numbers
Again, these are the times that it would take on average to restart our development server and load up our homepage.
thread-loaderfull page load, 232344.2ms (3m52s)cache-loaderfull page load, 137157.8ms (2m16s)hard-source-webpack-pluginfull page load, 85980.6ms (1m25s)
Goals
What we REALLY want to see is our webpack development server rebooting and serving requests (with a warmed cache) within 10 seconds. The 10 second number is arbitrary, but I think a good heuristic would be the time it takes to read all the modules from disk + some % of that time.
Looking over our modules for our client config, it took 1337.9 ms to read in and compute a sha265 hash for each module. If we assume for configuration differences that each of our configs needs to process all of their modules independently (there is major overlap), we can come up with an overall time of 2581ms (1337.9 + 974.3 + 100.6 + 168.4). Let's also assume that we need to read them twice (once for calculating a cacheable hash, and again for reading from cache), and that we're caching source maps so reading from cache will take twice as long. That'll put our build at 7743ms.
If we add in some overhead for verifying previous module resolutions, let's say 2 seconds assuming some improvements here based on this RFC issue. Build would now be at 9743ms.
Of course we need to account for bundle concatenation, module deduplication, scope hoisting, etc. but I believe that this is not where the bulk of the work is happening. Let's put another 50% on our build time.
I believe that 14,614ms should be a realistic upper limit for a cached build. Probably 20 seconds for a mostly cached build with a few files changed (this will prevent only optimizing for a full cache hit).
After many profiles, there don't seem to be many items left in terms of low hanging performance improvements.
What's the best way for us to contribute to making this a reality?
Additional considerations
We were able to build a tool internally as a proof of concept regarding maximum performance. Without a significant effort investment we were able to configure a simple bundler/server combo that compiled our entire build (incorrectly, so grain of salt) in ~20 seconds.
We did this basically by moving module transforming/parsing to worker threads and caching the results of the transforms. The main thread then builds the bundle dependency trees, and sends a sort of manifest to the workers for them to compile the final output.
I think that this approach would lead to the best performance possible, but it definitely comes with caveats. For one thing, instances cannot be shared amoung workers, so data passed to them must be serializable.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
gdborton commentedon Sep 26, 2017
❤️ the v4 milestone, but is there any way that we can help push this forward? I think that converting loaders/ast parsing to be purely functional, caching their results, and running them in workers by default get us a lot of the way there. I'd have no idea how to handle this though.
graingert commentedon Sep 27, 2017
is the source for this anywhere?
graingert commentedon Sep 27, 2017
is this a dupe of #1905?
gdborton commentedon Sep 28, 2017
It's not public, this was something that was developed ad-hoc internally to get more oomph out of the server.
It's certainly similar as both issues are about performance, but I like that this one includes specific performance goals. Also the scale for this issue is about 20x larger.
ljharb commentedon Sep 28, 2017
@sokra @TheLarkInn any update here? it'd be great to explore paths to making webpack performant enough for @airbnb's use cases :-)
rafde commentedon Sep 29, 2017
#5660 has performance updates. Maybe pulling that branch could help test perf changes. That or wait for beta tag.
gdborton commentedon Sep 29, 2017
I'll give it a shot and see how it impacts our build.
alexander-akait commentedon Oct 5, 2017
About
uglifyjs-webpack-plugin, i have PR that fix cache and parallels webpack-contrib/uglifyjs-webpack-plugin#108, it is allow to replacewebpack-parallel-uglify-pluginon basic plugin and welcome to feedback.alexander-akait commentedon Oct 5, 2017
Also about
cache-loader, he have was merged PR (webpack-contrib/cache-loader#5) that avoid fs calls and it is should be improve perf, but now new version with merged PR will not publish.joshwiens commentedon Oct 10, 2017
@gdborton -
[email protected]includes the performance improvements mentioned by @evilebottnawigdborton commentedon Oct 10, 2017
The uglify portion of the build is already very fast due to our caching w/
webpack-parallel-uglify-plugin, but it would certainly be nice to be back on the official plugin, but I don't think it'll get us any performance benefits.lencioni commentedon Oct 13, 2017
Also, just a little more context. We are currently attempting to enable the ModuleConcatenationPlugin, but are having problems running out of memory. Without this plugin, we are able to build by giving webpack 8 GiB of memory (
--max_old_space_size=8192), but when we enable this plugin after about 30 minutes of chugging along, we run out of memory even after bumping it to 30 GiB (--max_old_space_size=30720).Not sure if this is helpful, but here is some of the output:
Hopefully whatever work we end up doing to improve the speed of webpack will also help with the memory when this plugin is enabled, but it might be possible that we need to look at improvements in this plugin specifically as well.
TheLarkInn commentedon Oct 13, 2017
@ljharb The PR that @gdborton has open I'm keeping a close eye. I believe that the cases described in the initial writeup can be solved through parallelization at the Module#build level like Gary suggests in his PR notes.
53 remaining items
benneq commentedon Mar 7, 2018
What syntax you mean?
montogeek commentedon Mar 7, 2018
https://webpack.js.org/configuration/module/#rule-use
sod commentedon Mar 7, 2018
@benneq are you sure all those "thread-loader" things don't do more harm then good? The first post of this thread states that it made their build 60% slower. Should just try removing that.
And if you keep using it maybe incorporate the warning with it's interaction with the sass-loader as mentioned in the webpack doc.
And maybe remove all those minify configurations and use
webpack --mode=productionandwebpack --mode=development --watchinstead to boost development performance.https://medium.com/webpack/webpack-4-mode-and-optimization-5423a6bc597a
benneq commentedon Mar 7, 2018
@montogeek I'll check that soon.
@sod Yes, they improve our build times a lot.
In Webpack 3 our build times (for the whole project. Not only the webpack Build) went from 10 minutes to 7 minutes. Just by adding a few thread-loaders to the webpack config.
In Webpack 4 it's 14 minutes (without thread-loader) vs. 10 minutes.
As I said, we just left our webpack 3 config untouched and use it with webpack 4. Only removed the Uglify plugin.
Our start command for the config file I posted above is this:
I'm not really concerned about the watch mode. It went from 15 to 18 seconds startup time. That's acceptable. But 3 additional minutes on the build server are annoying.
I'm not quite sure which plugins and options I can remove from the config, because I don't know if all plugins already use the new development and production flags. Though I just kept everything as it was with webpack 3.
sokra commentedon Mar 9, 2018
Could you try without the ExtractTextWebpackPlugin?
benneq commentedon Mar 9, 2018
@montogeek : changing the syntax didn't change build times at all.
@sod : I removed webpack.LoaderOptionsPlugin. No improvement to build time. But bundle size increased by 5%. Though I'll keep the plugin (only in production build of course)
@sokra : We use ExtractTextWebpackPlugin v4.0.0-beta.0
I now just removed the lines concerning ExtractTextWebpackPlugin, but build time stays the same.
Is there some built in way in webpack 4 to generate external css file?
edmorley commentedon Apr 20, 2018
The recommendation with webpack 4 is to switch from extract-text-webpack-plugin to mini-css-extract-plugin (since the former has wontfixed official webpack 4 support, and the latter is more lightweight anyway).
dtothefp commentedon Apr 20, 2018
Seems like this issue has gone quiet since the release of Wepack 4 and that others are reporting performance problems when upgrading from v3 to v4 #6767. I upgraded a quite large project from v2 to v4 and am considering downgrading to v3 as our rebuild times are in the range of 30s to 2mins. I'm using
mini-css-extract-pluginand DLL Plugin along with HappyPack. Does anyone have any updates on Webpack v4 performance?alexander-akait commentedon Apr 21, 2018
@dtothefp can you provide link on your repo or create minimum reproducible test repo?
elevenpassin commentedon Apr 27, 2018
@dtothefp Preferably in a different issue I guess :D
Bnaya commentedon Apr 27, 2018
@dtothefp for dev, i'm swapping mini-css-extract-plugin with plain style-loader.
So i get HMR and much faster rebuilds.
Also, i'm on typescript project, and i set my ts-loader to transplieOnly, and i'm running
tscseparately,And my rebuilds are very fast (1s~)
abhinavsingi commentedon May 29, 2018
node version 8.9.4 should be used as webpack uses some optimisations done in that particular versions for build times
benneq commentedon May 29, 2018
What's the equivalent 10.x version where these optimizations are included?
sokra commentedon Jun 7, 2018
Sorry we can't watch every discussion in older issues. I better close this one so people will open new issues.
If you still have issue with rebuild performance here is a guide how to get it fixed: