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 package. In 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
😉
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:
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:
For the largest 5 clients, the unpacked publish size got reduced >50%:
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.
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.
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:
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.
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.
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.
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”.
We removed the source code from our published packages along with other dev/test configurations, which led to ~28% reduction in unpacked publish size.
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 sourceMap, inlineSourceMap and inlineSources are turned off by default.
We removed sourceMaps from v3, which led to ~20% reduction in unpacked publish size.
What was community’s reaction?
We had an overwhelming response from the Twitter community to these exciting news!
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.