AWS Developer Tools Blog

How we halved the publish size of modular AWS SDK for JavaScript clients

On December 15th, 2020, we announced the general availability of the AWS SDK for JavaScript, version 3 (v3). In v3, the modular packages reduce the bundle size of your application by ~75% as compared to that in AWS SDK for JavaScript, version 2 (v2). However, v3 had a large publish/install size for each modular packageIn this blog post, we cover how we reduced the publish size of v3 modular packages by ~50%.

Why did we do it?

The JavaScript community often jokes about the large size of node_modules 😉Joke on how node_modules is heavier than black hole, shown with space-time curvature diagram

As such, we are always looking for opportunities to reduce the size of our packages. While working with the AWS Lambda team to provide v3, we focused on reducing the install size of modular packages. There were improvement suggestions from the community, and we had backlog items to work on.

What did we accomplish?

We are happy to report that we reduced the publish size of v3 modular packages by ~50% in v3.36.1 as compared to that in v3.33.0!. As an effect, the install size for each client is also reduced by ~40%.

You can check the cost of installing @aws-sdk/* modular packages on packagephobia. The below screenshot for @aws-sdk/client-sts shows publish/install size reductions in versions leading to v3.36.1:

Screenshot of PackagePhobia showing install size reduction from 7.04 MB to 4.21 MB for STS Client of modular AWS SDK for JavaScript.

Install size reductions in top clients

The overall unpacked publish size reduction in clients ranges between 40% to 60%. For example, the graph below shows the unpacked publish size reduction for top 5 downloaded clients:

@aws-sdk/client-sso             : [███████████████░░░░░░░░░░░░░░░░░░░░░░] 41.39%
@aws-sdk/client-sts             : [█████████████████████░░░░░░░░░░░░░░░░] 58.09%
@aws-sdk/client-s3              : [██████████████████████░░░░░░░░░░░░░░░] 59.09%
@aws-sdk/client-cognito-identity: [██████████████████░░░░░░░░░░░░░░░░░░░] 49.94%
@aws-sdk/client-firehose        : [███████████████████░░░░░░░░░░░░░░░░░░] 51.11%

For the largest 5 clients, the unpacked publish size got reduced >50%:

@aws-sdk/client-ec2             : [████████████████████░░░░░░░░░░░░░░░░░] 53.85%
@aws-sdk/client-sagemaker       : [████████████████████░░░░░░░░░░░░░░░░░] 53.76%
@aws-sdk/client-iot             : [███████████████████░░░░░░░░░░░░░░░░░░] 51.63%
@aws-sdk/client-chime           : [███████████████████░░░░░░░░░░░░░░░░░░] 50.47%
@aws-sdk/client-rds             : [████████████████████░░░░░░░░░░░░░░░░░] 54.22%

node_modules size comparison between different versions

If you install a client in a fresh workspace and check the size of the package inside node_modules, you will see the disk-usage reduction in v3.36.1.

For example, installing @aws-sdk/client-sts@3.33.0 creates a node_modules with size of 8.9 MB. The client-sts is of size 1.4 MB and contains 115 files with 10054 lines of code.

$ npm install @aws-sdk/client-sts@3.33.0 --save-exact
...

$ du -sh --apparent-size node_modules                 
8.9M    node_modules

$ du -sh --apparent-size node_modules/@aws-sdk/client-sts
1.4M    node_modules/@aws-sdk/client-sts

$ npx cloc node_modules/@aws-sdk/client-sts 
     163 text files.
     160 unique files.                                          
      48 files ignored.
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                      45              0           1948           4674
TypeScript                      64            321           7506           4295
Markdown                         2           1679              0            825
JSON                             4              0              0            260
-------------------------------------------------------------------------------
SUM:                           115           2000           9454          10054
-------------------------------------------------------------------------------

The version @aws-sdk/client-sts@3.36.1 creates a node_modules with size of 5.7 MB. The client-sts is of size 603 KB and contains 85 files with 6585 lines of code.

$ npm install @aws-sdk/client-sts@3.36.1 --save-exact
...

$ du -sh --apparent-size node_modules
5.7M    node_modules

$ du -sh --apparent-size node_modules/@aws-sdk/client-sts
603K    node_modules/@aws-sdk/client-sts

$ npx cloc node_modules/@aws-sdk/client-sts 
      88 text files.
      86 unique files.                              
       3 files ignored.
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                      40              0              0           4383
TypeScript                      42            185           2481           1236
Markdown                         2           1711              0            840
JSON                             1              0              0            126
-------------------------------------------------------------------------------
SUM:                            85           1896           2481           6585
-------------------------------------------------------------------------------

How did we do it?

We created a copy of the client-s3 source code in trivikr/temp-client-s3. This allowed us to move fast, quickly implement and test ideas, and quantify the publish/install size reductions. We went through each file being published to npm in the client-s3 package and asked ourselves what role it plays. We brainstormed ideas and documented them in GitHub issues. Then, we implemented those ideas in descending order of return on investment.

We released each version of @trivikr-test/client-s3 with a specific change and documented the metrics from npm publish. We tested the debugging experience in trivikr/debug-temp-client-s3, and verified functionality in Node.js, browser and react-native environments in aws-samples/aws-sdk-js-tests#103.

We shared details about these improvements across multiple JavaScript interest channels including Github, Twitter, and even internal Slack rooms to get early feedback from the community.

We received overall positive feedback from developers:

Announcement of the AWS SDK for JavaScript team working on reducing install size of v3 on internal javascript interest slack channelComment from GitHub user Tim Kye affirming that removing source code reduces publish size.Comment from GitHub user Charlie Fish on experiments to reduce install/publish size look great.Screenshot of Twitter direct messages with AWS Serverless Hero Michael Hart discussing reduced install size

The key learnings are:

  • Provide developers with experimental artifacts to play with.
  • Ask specific questions to get responses.
  • Iterate on their feedback!

What did we change?

Once we quantified npm publish numbers for changes, we shortlisted the four best improvements to implement in v3:

  • We removed comments from transpiled *.js files.
  • We removed comments from downleveled *.d.ts files.
  • We removed TypeScript source code.
  • We removed source map files.

The v3 SDK is written in TypeScript programming language. TypeScript extends JavaScript by adding types, and saves you time catching errors and providing fixes before you run your code. We’ve covered in detail why we chose TypeScript in the blog post on first-class TypeScript support.

We removed comments from transpiled *.js files

We transpile TypeScript code to JavaScript in commonjs target for Node.js and es5 target for browser. We also ship types as a distribution in a different folder.

To assist customers, the services ship with extensive documentation for service and operations. We add this documentation in JSDoc comments. In our TSConfig setup, we shipped redundant comments in every distribution.

The JSDoc comments appear when you hover over Symbols in your code. In the below example, you see JSDoc for DynamoDB when hovering over the import.

Screenshot showing JSDoc comments for DynamoDB import on hover in a tooltip in VSCode editor.

This JSDoc is from *.d.ts files. We removed the redundant comments from transpiled *.js files and it led to ~6% reduction in unpacked publish size.

$ pwd
/home/trivikr/workspace/aws-sdk-js-v3/clients/client-sts

# Before the change
$ du -sh --apparent-size dist/cjs | cut -f1
301K

$ npx cloc dist/cjs
...
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                      22              0            974           2328
...

# After the change
$ du -sh --apparent-size dist/cjs | cut -f1 
239K

$ npx cloc dist/cjs
...
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                      22              0              0           1354
...

We removed comments from downleveled *.d.ts files

To support customers who use older versions of TypeScript, we downlevel types using downlevel-dts which converts code with new TypeScript features into code that uses equivalent old features. This feature adds duplicate comments in downleveled types which increases publish size.

We removed comments from downleveled *.d.ts files which led to ~10% reduction in unpacked publish size.

$ pwd
/home/trivikr/workspace/aws-sdk-js-v3/clients/client-sts

# Before the change
$ du -sh --apparent-size dist-types | cut -f1
742K

$ npx cloc dist-types
...
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
TypeScript                      59              0           4962           1813
...

# After the change
$ du -sh --apparent-size dist-types | cut -f1
396K

$ npx cloc dist-types
...
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
TypeScript                      59            185           2481           1813
...

As a consequence, customers using versions of TypeScript older than 4.0 will not see JSDoc comments in their IDE although the downleveled types will work. We went ahead with this change to encourage customers to switch to TypeScript 4.0+ which was released in August 2020.

If you are using TypeScript <4.0, the JSDoc comments will appear for SDK versions < 3.36.0.

We removed TypeScript source code

The authors of JavaScript libraries decide the language to write a library in based on variety of reasons. For example, we use TypeScript for v3 for reasons explained in the blog post on first-class TypeScript support. Other maintainers may choose a different language for writing their library: JavaScript, ReScript, PureScript, ClosureScript, CoffeeScript, Reason, Elm, Flow, etc. The consumer of their library does not have to know the language a library is written in. At the end, the engine processes JavaScript code.

To provide first-class TypeScript support, a library needs to ship types. If a library is not written in TypeScript, they either manually write types, or use TypeScript to generate type declarations.

We asked the question on Twitter if maintainers ship the source code in npm packages. Here is a quote from one of the replies: “Shipping source code is going against the spirit of module definition”.

Response from Twitter user Harshal Patil on how shipping source code is going against the spirit of module definition.

We removed the source code from our published packages along with other dev/test configurations, which led to ~28% reduction in unpacked publish size.

$ pwd
/home/trivikr/workspace/aws-sdk-js-v3/clients/client-sts

# Before the change
$ npm pack --dry-run
...
package size:  141.7 kB                                
unpacked size: 1.2 MB                                  
total files:   177
...

# After the change
$ npm pack --dry-run
...
package size:  99.9 kB                                 
unpacked size: 783.8 kB                                
total files:   88 
...

We removed source map files

Source map files allow debuggers and other tools to display the original TypeScript source code when actually working with the emitted JavaScript files. In TypeScript, the source map files are emitted as .js.map (or .jsx.map) files next to the corresponding .js output file. TypeScript also allows embedding the source map content in the .js files. TypeScript also allows for inclusion of the original content of the .ts file as an embedded string in the source map.

The source map files are helpful with debugging the application code. They are not useful for libraries and dependencies who have to meet strict publish/install size restrictions. A better solution is to release debug versions of the SDK. If you have feedback about source maps, or would like to explain your debugging or other use cases, please comment on GitHub issue aws/aws-sdk-js-v3/#2895.

In the TypeScript publishing guide, there is no recommendation on publishing source maps. In TSConfig, the settings for sourceMapinlineSourceMap and inlineSources are turned off by default.

We removed sourceMaps from v3, which led to ~20% reduction in unpacked publish size.

$ pwd
/home/trivikr/workspace/aws-sdk-js-v3/clients/client-sts

# Before the change
$ du -sh --apparent-size dist-cjs | cut -f1
246K

$ npx cloc dist-cjs
...
      42 text files.
      42 unique files.                              
      21 files ignored.
...

# After the change
$ du -sh --apparent-size dist-cjs | cut -f1
174K

$ npx cloc dist-cjs
...
      21 text files.
      21 unique files.                              
       0 files ignored.
...

What was community’s reaction?

We had an overwhelming response from the Twitter community to these exciting news!

Announcement on twitter about install/publish size reduction by ~35% in modular AWS SDK for JavaScript.Responses and questions from Twitter users on reduction in install/publish size.Responses and questions from Twitter users on reduction in install/publish size.Responses and questions from Twitter users on reduction in install/publish size.

Join the conversation on Twitter and let us know how you have reduced publish/install/bundle sizes in your npm packages or any other experiences you’ve had with AWS SDK for JavaScript.

What do we plan to do in the future?

To ensure that we keep bloat from sneaking back in, we plan to add CI to check publish size.

We’ve not reduced the size of the actual source code yet. For example, a generic function for API calls will reduce the size of the source code by ~0.5%. If you have any ideas to reduce publish size, do post them in our experimental repo at trivikr/temp-client-s3/issues.

We have also not considered using advanced or alternative compilation options like Google Closure Compiler, Babel or SWC. If you have ideas/suggestions or examples of how they can help, please comment on GitHub issue aws/aws-sdk-js-v3/#2897.

We are also considering shipping Node.js specific distributions and a type definitions distribution in separate prerelease version numbers, which could further reduce npm install size from between 60% and 75%. Although the expected improvements are huge, it would take lot of discussion and testing prior to implementation. Please post your ideas/suggestions/concerns on GitHub Issue aws/aws-sdk-js-v3/#2889.

If you have a feedback about TypeScript source code and source maps, or would like to explain your debugging or other use cases, please comment on GitHub issue aws/aws-sdk-js-v3/#2895.

How can you contribute?

We value your feedback, so please tell us what you like and don’t like by opening an issue on GitHub. Reducing install/publish size is a two-way doors decision. Do inform us on GitHub if it breaks your developer experience.

Trivikram Kamat

Trivikram Kamat

Trivikram is maintainer of AWS SDK for JavaScript in Node.js and browser. Trivikram is also a Node.js Core collaborator and have contributed to HTTP, HTTP/2 and HTTP/3 over QUIC implementations in the past. He has been writing JavaScript for over a decade. You can find him on Twitter @trivikram and GitHub @trivikr.