Why did we do it?
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
@email@example.com 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.
@firstname.lastname@example.org 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 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
- We removed comments from downleveled
- We removed TypeScript source code.
- We removed source map files.
We removed comments from transpiled
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
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
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
.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.
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!
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
%. 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.