Skip to content

Performance - Reduce build time and memory usage #4765

Open

Description

@slorber

💥 Proposal

With Webpack 5 support, re-build times are now faster.

But we still need to improve the time for the first build which is not so good currently.

Some tools to explore:

It will be hard to decouple Docusaurus totally from Webpack at this point.

But we should at least provide a way for users to use an alternative (non-Babel) JS loader that could be faster and good enough. Docusaurus core should be able to provide a few alternate loaders that would work by default using the theme classic, by just switching a config flag.

If successful and faster, we could make one of those alternate loader the default loader for new sites (when no custom babel config is found in the project).

Existing PR by @SamChou19815 for esbuild: #4532

Activity

  1. added
    proposal
    on May 11, 2021
  2. changed the title [-]Decrease build time[/-] [+]Decrease build time - use alternative webpack JS loaders[/+] on May 11, 2021
  3. changed the title [-]Decrease build time - use alternative webpack JS loaders[/-] [+]Reduce build time - use alternative webpack JS loaders[/+] on May 11, 2021
  4. slorber commented on May 14, 2021

    @slorber
    CollaboratorAuthor

    For anyone interested, we added the ability to customize the jsLoader here #4766

    This gives the opportunity to replace babel by esbuild, and you can add this in your config:

      webpack: {
        jsLoader: (isServer) => ({
          loader: require.resolve('esbuild-loader'),
          options: {
            loader: 'tsx',
            format: isServer ? 'cjs' : undefined,
            target: isServer ? 'node12' : 'es2017',
          },
        }),
      },

    We don't document it yet (apart here).
    We may recommend it later for larger sites if it proves to be successful according to feedback from early adopters, so please let us know if that works for your use-case.

    Important notes:

  5. adventure-yunfei commented on Jun 15, 2021

    @adventure-yunfei
    Contributor

    came from #4785 (comment).

    Just wondering, is this issue aiming to reduce build time for entire site generator (including md/mdx parser, or Docs), or just jsx react pages?

  6. slorber commented on Jun 15, 2021

    @slorber
    CollaboratorAuthor

    @adventure-yunfei md docs are compiled to React components with MDX, and the alternative js loader like esbuild also process the output of the MDX compiler, so this applies to documentation as well. Check the mdx playground to test the mdx compiler: https://mdxjs.com/playground/

    If you have 10k docs you basically need to transpile 10k react components

  7. adventure-yunfei commented on Jun 15, 2021

    @adventure-yunfei
    Contributor

    @slorber perfect! we're also trying to use esbuild to boost build time (for application project). I'll have a try on this.

    BTW I've created a similar large doc site here from our internal project.

    Update:
    Tested with a higher perf PC:

    • with Docusaurus 2.0.0-beta.0, doc site generation finished in 63min
    • with latest in-dev version, doc site generation finished in 30min.
      Reduced 50% time. 👍
  8. alphaleonis commented on Jun 23, 2021

    @alphaleonis

    This gave a nice performance boost, although I think there are still more to be desired. Out of curiosity, what is actually going on that is taking so much time behind the scenes? In our case (a site with around 2,000 .md(x) files) most of the time spent seems to be before and after the "Compiling Client/Compiling Server" progress bars appear and complete.

    As it stands, building the site takes around 20 minutes with esbuild, and was closer to 40 minutes before. Then out of curiosity, I just tested to add four versions to our site, and building it. Before using esbuild, the process took just shy of 13 hours(!). Using esbuild it was down to just shy of 8 hours. (Still way too long to be acceptable). So while it was a big improvement, it still seems to be very slow.

    In the second case, it reported:

    [success] [webpackbar] Client: Compiled successfully in 1.33h
    [success] [webpackbar] Server: Compiled successfully in 1.36h
    

    What was going on for the remaining 5 hours? Is this normal behavior, or did we configure something incredibly wrong? And why does it take much longer than four times the amount of time with four versions added?

  9. slorber commented on Jun 23, 2021

    @slorber
    CollaboratorAuthor

    @alphaleonis it's hard to say without further analysis, but the MDX compiler is transforming each md doc to a React component, that is later processed by babel (or esbuild).

    The MDX compiler might be a bottleneck, this is why I'd like to provide an alternate MD parser for cases where MDX is not really needed.

    Webpack might also be a bottleneck.

    Using esbuild is not enough. Also when using esbuild as a webpack loader, we are not really leveraging the full speed benefits of esbuild. Unfortunately we can't replace replace Webpack by esbuild easily, it's part of our plugin lifecycle API and Webpack is more featured than esbuild (we use various things like file-loader, svgr loader...)

    What was going on for the remaining 5 hours? Is this normal behavior, or did we configure something incredibly wrong?

    We have enabled Webpack 5 has persistent caching, and rebuild times are much faster. You need to persist node_modules/.cache across build to leverage it.

    And why does it take much longer than four times the amount of time with four versions added?

    It's hard to tell without measuring on your system. Your system may have not enough memory for Webpack to do its job efficiently, leading to more garbage collection or whatever.

  10. alphaleonis commented on Jun 23, 2021

    @alphaleonis

    @slorber Thanks for the explanation. We did try the persistent caching, and it seems to help a lot with the time spent during the "Build server/client" phase (which I assume is Webpack). The machine in question had 16GB memory, and the same was specified as max_old_space_size.

    Is there any way we can do some further analysis, such as enabling some verbose logging to get some more details perhaps? Or is this kind of the expected build time for sites of that size? (If so I guess we will have to find another solution for versioning, such as building/deploying each version separately.

  11. johnnyreilly commented on Jun 23, 2021

    @johnnyreilly
    Contributor

    Also when using esbuild as a webpack loader, we are not really leveraging the full speed benefits of esbuild

    This is true - but there's still a speed benefit to take advantage of. It's also pretty plug and play to make use of. See my post here:

    https://blog.logrocket.com/webpack-or-esbuild-why-not-both/

  12. slorber commented on Jun 24, 2021

    @slorber
    CollaboratorAuthor

    Is there any way we can do some further analysis, such as enabling some verbose logging to get some more details perhaps?

    This is a Webpack-based app, and the plugin system enables you to tweak the Webpack config to your needs (configureWebpack lifecycle) and add logs or whatever you want that can help troubleshoot the system. You can also modify your local docusaurus and add tracing code if you need.

    I'm not an expert in Webpack performance debugging so I can't help you much on how to configure webpack and what to measure exactly, you'll have to figure out yourself for now.

    Or is this kind of the expected build time for sites of that size?

    It's hard to have meaningful benchmarks. Number of docs is one factor but also the size of docs obviously matter so one site is not strictly comparable to another. 40min build time for 2000 mdx docs with babel seems expected when comparing with other sites. Obviously it's too much and we should aim to reduce that build time, but it's probably not an easy thing to do.

    (If so I guess we will have to find another solution for versioning, such as building/deploying each version separately.

    For large sites, it's definitively the way to go, and is something I'd like to document/encourage more in the future.
    It's only useful to keep multiple versions in master when you actively update them.
    Once a version becomes unmaintained, you should rather move it to a branch and create a standalone immutable deployment for it, so that your build time does not increase as time passes and your version number increase.

    We have made it possible to include "after items" in the version dropdown, so that you can include external links to older versions, and we use it on the Docusaurus site itself:

    image

    I also want to have a "docusaurus archive" command to support this workflow better, giving the ability to publish a standalone version of an existing site and then remove that version.

  13. adventure-yunfei commented on Jun 26, 2021

    @adventure-yunfei
    Contributor

    Tested with a higher perf PC:

    • with Docusaurus 2.0.0-beta.0, doc site generation finished in 63min
    • with latest in-dev version, doc site generation finished in 30min.
      Reduced 50% time. 👍

    Saddly the process costs a very large memory.
    My local testing environment has 32G memory, but in CICD environment memory limit is 20G. The process is killed cause of OOM, during emitting phase. From the monitor, the memory suddenly increased from 8G to 20G+.

  14. slorber commented on Jun 26, 2021

    @slorber
    CollaboratorAuthor

    It is unexpected that beta.2 is faster than beta.0, maybe you didn't clear your cache?

    The process is killed cause of OOM, during emitting phase. From the monitor, the memory suddenly increased from 8G to 20G+.

    What do you mean by the "emitting phase"? I didn't take much time to investigate all this so any info can be useful.

  15. adventure-yunfei commented on Jun 27, 2021

    @adventure-yunfei
    Contributor

    It is unexpected that beta.2 is faster than beta.0, maybe you didn't clear your cache?

    I'm using the esbuild-loader config from the docusaurus website example. So it should be esbuild making build faster.

    What do you mean by the "emitting phase"? I didn't take much time to investigate all this so any info can be useful.

    This may not be accurate. The process memory was 7G most times. About 20 minutes later memory jumped to 20.2G while the console showing Client "emitting". After client build finished, the memory dropped down to 7G. (The Server was still building)

  16. 119 remaining items

  17. Romej commented on Jul 25, 2024

    @Romej

    I would love to use docusaurus with rspack, we had many legacy CRA projects and we successfully migrated all of them to rspack without much efforts.

    with rspack 1.0 around the corner, i am hoping this will be an option soon

  18. clainchoupi commented on Sep 4, 2024

    @clainchoupi
  19. OzakIOne commented on Sep 4, 2024

    @OzakIOne
  20. clainchoupi commented on Sep 5, 2024

    @clainchoupi
  21. slorber commented on Sep 5, 2024

    @slorber
    Author
  22. changed the title [-]Performance - Reduce build time and memory usage - use alternative webpack JS loaders[/-] [+]Performance - Reduce build time and memory usage[/+] on Oct 17, 2024
  23. slorber commented on Oct 22, 2024

    @slorber
    CollaboratorAuthor

    FYI Docusaurus v3.6 is entirely focused on performance - see the Docusaurus Faster project

    The "faster" option is opt-in. It's a breaking change only if you turn it on, although most sites should be able to adopt it relatively easily.

    Activating this option should have a significant impact on build time and memory consumption.

    You can try a Docusaurus canary right now and let me know how fast it is. It is relatively stable and ready to release, I'm just polishing the last details and writing docs before tagging v3.6.

  24. slorber commented on Nov 4, 2024

    @slorber
    CollaboratorAuthor
  25. Airkro commented on May 15, 2025

    @Airkro

    Docusaurus Faster is still not stable.

    Please consider using options.experiments.lazyCompilation of webpack. It makes everything faster when in dev mode.

    Only need more <Suspense> to support it better.

  26. slorber commented on May 15, 2025

    @slorber
    CollaboratorAuthor

    @Airkro, it won't be stable until Docusaurus v4 because it's a breaking change anyway, Rspack is not 100% retrocompatible with webpack, and we can't guarantee a seamless upgrade for absolutely all of our sites. It doesn't mean it doesn't work well, and many large sites have already adopted it successfully.

    I'm also still working on performance improvements, so it may even be a bit too soon for Docusaurus v4 to use Docusaurus Faster by default, considering I'm still working on improvements and we need community feedback/adoption. If we turn on Docusaurus Faster by default, we'll likely provide an opt out to restore former behavior.


    You can always tweak our default webpack client/server dev/prod configurations, thanks to the plugin lifecycle methods

    function myPlugin() {
      return {
        name: "my-plugin",
        configureWebpack() {
          return {
            experiments: {
              lazyCompilation: true,
            },
          };
        },
      };
    }

    Last time I tried using lazyCompilation, it didn't work well at all; the site couldn't build, and the errors were unhelpful in troubleshooting the problem. If you were able to run your site with this option, please let me know how or provide a repro. I'm looking to adopt it for Docusaurus.

  27. Airkro commented on May 15, 2025

    @Airkro
    if (process.env.WATCHING) { // custom detecting
      return {
        experiments: {
          lazyCompilation: {
            imports: true,
            entries: true
          }
        }
      }
    }

    Only using lazyCompilation in development mode (docusaurus start) can delay many unnecessary builds. It only works with devServer, not for building, but it still helps when the site becomes bigger and slower.

    You may see page loading fail or blank pages(can be fixed by page refresh), but it will usually work fine.
    If the docusaurus framework uses more React.lazy + Suspense fallback, it will have more obvious improvements.

  28. CMCDragonkai commented on Mar 6, 2026

    @CMCDragonkai

    I came across this error #8329 in the Github CI/CD running docusaurus build.

    What's actually scaling docusaurus's webpack phase memory usage? Is it the number of assets going through the webpack pipeline? If so increasing the heap memory will just fail once there's too many blog pages that feature some image or something. That's not really scalable.

    What's the solution here, only use static URL paths and avoid the webpack pipeline? Or some way of making webpack discard un-used memory as much as possible?

  29. slorber commented on Mar 6, 2026

    @slorber
    CollaboratorAuthor

    @CMCDragonkai if you encounter a perf issue on a site it would be helpful to open a separate issue with all the details, otherwise it's really hard to do anything. If the site is https://polykey.com/docs then the first step would be to upgrade it to our latest release and turn on all the Docusaurus Faster options. I have no idea what you mean by "static URL paths".

  30. CMCDragonkai commented on Mar 7, 2026

    @CMCDragonkai

    I shifted all static assets out of the webpack pipeline by referencing via pathname. I benchmarked webpacks memory usage too and it appears to be dropped by 1GiB. But it was getting bigger and bigger the more assets (images mainly I was referencing in docusaurus). So I actually think the webpack pipeline is only good for UI assets, for non-UI assets like content assets it has to use the static paths. And do direct cachebusting.

    Yes I'm in docusaurus 3.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    domain: performanceproposal

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions